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
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
- Android:STATE_SELECTED不工作 2022-01-01
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
- 我的 UIImageView 的任务 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
- 如何将多个组件添加到 PickerView? 2022-01-01
