Change the color of the text when the user types?

综合技术 2017-12-31

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!

责编内容by:Hello, buddy! (源链)。感谢您的支持!

您可能感兴趣的

Android 开发技术周报 Issue#183 新闻 Google推送第三个Android P开发者预览版 正式版将于8月到来 Android P Beta 2发布:引入小龙虾和草泥马等157个Emoji ...
Android 仿支付宝城市服务栏目tab选择滑动子View效果... 一. 图示 支付宝效果 实现的效果 二. 思路讲解 TabLayout+ScrollView实现即可。每一个tab对应scrollview中包裹的一层布局,以上有4个tab,也就需要inflate 4个布...
Android源码分析:手把手带你分析 Glide的缓存功能... 前言 Glide ,该功能非常强大 Android 图片加载开源框架 相信大家并不陌生 正由于他的功能强大,所以它的源码非常复杂,这导致很多人望而却步 本人尝试将 Glide 的功能进行分解,并单独针对每个功能进行源码...
Track Satellites With Android Track Satellites With Android Written by Harry Fairhead Monday, 06 November 2017 ...
全球第一个云端WebRTC编译系统 全球第一个云端WebRTC编译系统 -- The first cloud webrtc compliation system in the world, you can contact me if you want to use all over the world. 目前有WebR...