Change the color of the text when the user types?

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?

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

EditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

}

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”);

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Change the color of the text when the user types?

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录