安卓的EditText +设置颜色有些话颜色、EditText

2023-09-06 07:16:11 作者:流年瓦解我们的记忆ミ

我动态创建的EditText框,这取决于被拉从数据库返回的。

I create EditText boxes dynamically, depending on what is pulled back from the database.

IE浏览器。

            final EditText textV = new EditText(this);
            textV.setText(monEntry);

反正是有一些设置一种颜色到另一个在EditText上的文本和另一位的,而无需使用一个子()? 非常感谢,如果有人能帮助!

Is there anyway to set some of the text in the EditText to one colour and another bit to another without using a subString()? Thanks alot if anybody can help!

推荐答案

是的,你可以有不同颜色的文字不同的地方,如果你使用的是SpannableString。例如:

Yes, you can have different colors in different places of the text if you are using SpannableString. Example:

SpannableString text = new SpannableString("Lorem ipsum dolor sit amet");  
final EditText textV = new EditText(this);
// make "Lorem" (characters 0 to 5) red  
textV.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);  
textV.setText(text, BufferType.SPANNABLE);

也可以使用HTML code如下::

or you can use html code as below::

textV.setText(Html.fromHtml(html text having 1 in red 2 in green and so on));

还有一个更完整的例子这里。

Javadoc文档 SpannableString

Javadoc for SpannableString