第一种方法 :
String num="您好!您在2014年04月23日15:20发布的座驾"+"<font color =\"#00ccff\">"+"您带来的不便,请您给予理解,官方消息,无需回复谢谢"+"</font>"+"buzhidao sjhemajsadja";
tv3.setText(Html.fromHtml(num));
第二种方法“
SpannableStringBuilder style=new SpannableStringBuilder(text);
style.setSpan(new ForegroundColorSpan(Color.RED),index[0],index[0]+1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.RED),index[1],index[1]+2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
style.setSpan(new BackgroundColorSpan(Color.RED),index[2],index[2]+3,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
tv3.setText(style);
TextView tv = (TextView) findViewById(R.id.textView2);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "变色了", 1).show();
}
});
}
}
本文介绍了如何通过HTML和Java代码实现文本颜色、背景色的变化,并在点击时触发特定操作。具体包括使用字符串拼接、SpannableStringBuilder类设置样式以及在TextView上设置监听器。
431

被折叠的 条评论
为什么被折叠?



