Android EditText + set some words in colour(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 + 设置一些颜色的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android EditText + 设置一些颜色的单词
基础教程推荐
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
