Android EditText + 设置一些颜色的单词 [英] Android EditText + set some words in colour

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

问题描述

我根据从数据库中提取的内容动态创建 EditText 框.

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

即.

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

有没有在不使用 subString() 的情况下将 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代码::

or you can use html code as below::

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

这里有一个更完整的例子.

SpannableString

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

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