当用户键入时更改文本颜色?
当用户在 EditText 中输入时,我希望某些关键字改变颜色。 当应用程序发现用户输入关键字并使用 Html.fromHtml 时,我可以在关键字周围放置 HTML 标签,但用户将自己输入真正的 HTML 标签,这会弄乱它。所以我想我应该使用 Spannable 吗?
我到底应该如何扫描 EditText 来查找这些关键字?意思是——什么是最有效的方法?我在想也许是所有关键字的数组 - 然后循环查看是否找到任何匹配项。关于如何解决这个问题有什么想法吗?
While the user is typing in an EditText I would like certain keywords to change colors.
I could place HTML tags around the keywords when the app discovers the user typed it and use Html.fromHtml but the user will be entering real HTML tags themselves and that would mess it up. So I guess I should use Spannable?
And how exactly should I scan the EditText for these keywords? Meaning - what is the most efficient way? I was thinking maybe an array of all the keywords - and then loop through and see if any matches are found . Any ideas on how to approach this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您好,在 EditText 上使用 TextWatcher 查看用户正在输入的内容,并使用 Linkify 检查模式。
https://developer.android.com/reference/android/text/util /Linkify.html
}
使用 onTextChanged 侦听器检查用户正在输入的内容。
模式匹配示例..
int flags = Pattern.CASE_INSENSITIVE;
模式 p = Pattern.compile("\[0-9]*\", flags);
Linkify.addLinks(myTextView, p,
“内容://com.foo”);
Hi Use TextWatcher on EditText to see what user is typing and use Linkify to check pattern.
https://developer.android.com/reference/android/text/util/Linkify.html
}
Use onTextChanged Listener to check what user is typing.
Sample for Pattern matching..
int flags = Pattern.CASE_INSENSITIVE;
Pattern p = Pattern.compile("\[0-9]*\", flags);
Linkify.addLinks(myTextView, p,
"content://com.foo");
创建一个模式匹配器并将模式与 onTextChanged 侦听器中的文本进行匹配
}
Create a Pattern Matcher and match pattern with Text in onTextChanged Listener
}