实现自动提示输入框AutoCompleteTextView
KaceyTan
8年前
<ul> <li>AutoCompleteTextView继承EditText,默认为输入2个字符才出现提示</li> <li>MutiAutoCompleteTextView继承AutoCompleteTextView,使用 <strong>setTokenizer</strong> 设置分词方式(CommaTokenizer)</li> <li>设置","逗号为分隔符: <p>setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer())</p> </li> </ul> <p><strong>资源文件</strong></p> <pre> <code class="language-java"><string-array name="hints"> <item>who</item> <item>where</item> <item>what</item> <item>how</item> <item>foot</item> <item>city</item> <item>cupcake</item> <item>mashmallow</item> <item>nougat</item> </string-array></code></pre> <p><strong>布局文件</strong></p> <pre> <code class="language-java"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="cn.ucai.day09_12_08_adapterview.AutoActivity_12_11"> <AutoCompleteTextView android:id="@+id/autoTextView" android:layout_width="match_parent" android:layout_height="30dp" /> <MultiAutoCompleteTextView android:id="@+id/multiTextView" android:layout_width="match_parent" android:layout_height="30dp" /> </LinearLayout></code></pre> <p><strong>两个自动补全输入框效果图</strong></p> <p style="text-align: center;"><img src="https://simg.open-open.com/show/1521de584af5a34f1e2bf50cb935587c.png"></p> <p style="text-align:center">布局.png</p> <p><strong>AutoTextView输入框效果图</strong></p> <p style="text-align: center;"><img src="https://simg.open-open.com/show/fe6823ba7d1d0462af08ea3f6c9737bc.png"></p> <p style="text-align:center">AutoTextView.png</p> <p><strong>MutiAutoTextView输入框效果图</strong></p> <p style="text-align: center;"><img src="https://simg.open-open.com/show/566f02835ba52163992c59fcd9456d77.png"></p> <p style="text-align:center">MutiAutoTextView.png</p> <p><strong>实现java代码</strong></p> <pre> <code class="language-java">String hints[]=getResources().getStringArray(R.array.hints); AutoCompleteTextView autoCompleteTextView= (AutoCompleteTextView) findViewById(R.id.autoTextView); MultiAutoCompleteTextView multiAutoCompleteTextView= (MultiAutoCompleteTextView) findViewById(R.id.multiTextView); ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,hints); autoCompleteTextView.setAdapter(adapter); multiAutoCompleteTextView.setAdapter(adapter); //设置","逗号去分割 multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());</code></pre> <p> </p> <p>来自:http://www.jianshu.com/p/7a00295910fc</p> <p> </p>