美文网首页Android开发Android知识Android技术知识
实现自动提示输入框AutoCompleteTextView

实现自动提示输入框AutoCompleteTextView

作者: dayang | 来源:发表于2016-12-11 21:33 被阅读802次
    • 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());
    

    相关文章

      网友评论

        本文标题:实现自动提示输入框AutoCompleteTextView

        本文链接:https://www.haomeiwen.com/subject/cgbpmttx.html