实现自动提示输入框AutoCompleteTextView

  • AutoCompleteTextView继承EditText,默认为输入2个字符才出现提示
  • MutiAutoCompleteTextView继承AutoCompleteTextView,使用setTokenizer设置分词方式(CommaTokenizer)
  • 设置","逗号为分隔符:

setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer())

资源文件
<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>
布局文件
<?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>
两个自动补全输入框效果图
布局.png
AutoTextView输入框效果图
AutoTextView.png
MutiAutoTextView输入框效果图
MutiAutoTextView.png
实现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());
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容