[android]AutoCompleteTextView-输入内容自动完成文本框

Android 4.0

一、AutoCompleteTextView
 

AutoCompleteTextView和EditText组件类似,都可以输入文本。
但AutoCompleteTextView组件可以和一个字符串数组或List对象绑定,
当用户输入两个及以上字符时,系统将在AutoCompleteTextView组件下方列出字符串数组中所有以输入字符开头的字符串,这一点和www.google.com的搜索框非常相似,
当输入某一个要查找的字符串时,google搜索框就会列出以这个
字符串开头的最热门的搜索字符串列表。

在main.xml文件中配置:

<AutoCompleteTextView

   android:layout_width="fill_parent“  android:layout_height="wrap_content

  <!– completionThreshold 指定至少输入几个字符后才会出现自动提示功能 ,1表示输入一个字提示à

   android:completionThreshold="1“ 

   android:id="@+id/name" />




二、MultiAutoCompleteTextView


以逗号分隔,其中逗号为英文的逗号。

除了AutoCompleteTextView控件外,我们还可以使用MultiAutoCompleteTextView控件来完成连续输入的功能。也就是说,当输入完一个字符串后,在该字符串后面输入一个逗号(,),在逗号前后可以有任意多个空格,然后再输入一个字符串,仍然会显示自动提示列表。
使用MultiAutoCompleteTextView时,需要为它的setTokenizer方法指
定MultiAutoCompleteTextView.CommaTokenizer类对象实例,
该对象表示采用逗号作为输入多个字符串的分隔符

在main.xml文件中配置:
<MultiAutoCompleteTextView
    android:id="@+id/multi_name"    
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:completionThreshold="1" />

三、示例代码:

 public void onCreate(Bundle savedInstanceState) {
         
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
       
        autotext = (AutoCompleteTextView) this.findViewById(R.id.name);
        multiautotext =  (MultiAutoCompleteTextView) this.findViewById(R.id.multi_name);
       
        String[] names = {"老张","老方","老毕","李理","曾凡胜"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,names);
        autotext.setAdapter(adapter);
       
        multiautotext.setAdapter(adapter);
        multiautotext.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());