android点击文字颜色,android textview 部分文字加颜色并可点击

package com.example.urlspantextviewclick;

import android.os.Bundle;

import android.app.Activity;

import android.graphics.Color;

import android.text.Html;

import android.text.Spannable;

import android.text.SpannableString;

import android.text.Spanned;

import android.text.method.LinkMovementMethod;

import android.text.style.ClickableSpan;

import android.text.style.ForegroundColorSpan;

import android.view.Menu;

import android.view.View;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity {

private TextView t1;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

t1=(TextView)findViewById(R.id.txt);

CharSequence str=getText(R.string.hello_world);

SpannableString spannableString1 = new SpannableString(str);

spannableString1.setSpan(new ClickableSpan(){

@Override

public void onClick(View widget) {

Toast.makeText(getApplicationContext(), "who hit me", Toast.LENGTH_SHORT).show();

}

}, str.length()-3, str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

spannableString1.setSpan(new ForegroundColorSpan(Color.RED),str.length()-3, str.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

t1.setText(spannableString1);

t1.setMovementMethod(LinkMovementMethod.getInstance());

//Html.fromHtml(source)

//spannableString1.

/**to see http://www.cnblogs.com/mxgsa/archive/2012/11/15/2760256.html

* Android TextView 支持的HTML标签

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

*/

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

?

原文:http://913.iteye.com/blog/2217047

你可能感兴趣的:(android点击文字颜色)