如何获得一个红色星号在<字符串>条目星号、条目、字符串、如何获得

由网友(志在四方)分享简介:你如何获得一个红色星号的条目,以便您可以在文本的末尾显示出来,以表明其必填字段,比如:输入你的名字*(星号会变成红色)。或者,对于这个问题,文中的任何位置。 How do you get a red asterisk in a entry so that you can display it at the end...

你如何获得一个红色星号的条目,以便您可以在文本的末尾显示出来,以表明其必填字段,比如:输入你的名字*(星号会变成红色)。或者,对于这个问题,文中的任何位置。

How do you get a red asterisk in a entry so that you can display it at the end of the text to indicate its a required field, like: Enter your name * (asterisk will be red). Or, for that matter, anywhere within the text.

推荐答案

您不能这样做,通过XML字符串资源。这只能通过code来完成。为此,您需要使用SpannableStringBuilder和ForegroundColorSpan.

You can't do that through xml string resources. This can only be done via code. For this you need to use SpannableStringBuilder and ForegroundColorSpan.

下面是简单的例子:

TextView text = (TextView)findViewById(R.id.text);

String simple = "Enter your 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);

text.setText(builder);

阅读全文

相关推荐

最新文章