TextView text = (TextView)findViewById(R.id.text);
String simple = "Enter your name ";
String colored = "*";
SpannableStringBuilder builder = new SpannableStringBuilder(simple+colored);
builder.setSpan(new ForegroundColorSpan(Color.RED), simple.lenth(), builder.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setText(builder);
使用以下代码在strings.xml文件中实现此功能最简单,然后在color.xml文件中提供颜色即可。
<string name="user_name">UserName <font color='red'>*</font></string>
TextView txt_name = (TextView) findViewById(R.id.text);
String simple = "Name";
String colored = "*";
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(simple);
int start = builder.length();
builder.append(colored);
int end = builder.length();
builder.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txt_name.setText(builder);
SpannableString
。 - N J