安卓的EditText +设置颜色有些话 [英] Android EditText + set some words in colour

查看:229
本文介绍了安卓的EditText +设置颜色有些话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态创建的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

这篇关于安卓的EditText +设置颜色有些话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆