[Android] AutoCompleteTextView:自动完成输入内容的控件(自动补全)

 AutoCompleteTextView是EditText的直接子类,与普通EditText的最大不同就是,在用户输入的过程中,可以列出可供选择的输入项,方便使用者。

        AutoCompleteTextView与普通EditText控件使用方法类似,只是需要为其指定一个Adapter对象,绑定可供选择的输入项。

        AutoCompleteTextView可实现一次自动完成的功能,而另一个控件MultiAutoCompleteTextView,可以连续多次自动完成,即在通过自动完成一个输入项,接着输入一个分隔符后,继续通过自动完成连续输入多个输入项。只是要使用MultiAutoCompleteTextView类的setTokenizer方法指定分割符。

        两种自动完成输入内容的控件实例如下。


Main.java

[java]  view plain copy
  1. <span style="font-size:18px;">package mobile.android.ch05.autotext;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.ArrayAdapter;  
  6. import android.widget.AutoCompleteTextView;  
  7. import android.widget.MultiAutoCompleteTextView;  
  8.   
  9. public class Main extends Activity  
  10. {     
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState)  
  13.     {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         String[] autoString = new String[]  
  17.         { "联合国""联合国安理会""联合国五个常任理事国""bb""bcd""bcdf""Google",  
  18.                 "Google Map""Google Android" };  
  19.         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,  
  20.                 android.R.layout.simple_dropdown_item_1line, autoString);  
  21.           
  22.         // AutoCompleteTextView  
  23.         AutoCompleteTextView autoCompleteTextView =  
  24.                 (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);  
  25.         autoCompleteTextView.setAdapter(adapter);     // 绑定adapter  
  26.           
  27.         // MultiAutoCompleteTextView  
  28.         MultiAutoCompleteTextView multiAutoCompleteTextView =  
  29.                 (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView);  
  30.         multiAutoCompleteTextView.setAdapter(adapter);  
  31.         multiAutoCompleteTextView  
  32.                 .setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());  
  33.     }  
  34. }</span>  


main.xml

[html]  view plain copy
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <TextView  
  6.         android:layout_width="fill_parent"  
  7.         android:layout_height="wrap_content"  
  8.         android:text="AutoCompleteTextView" />  
  9.     <AutoCompleteTextView  
  10.         android:id="@+id/autoCompleteTextView"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content" />  
  13.     <TextView  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="MultiAutoCompleteTextView" />  
  17.     <MultiAutoCompleteTextView  
  18.         android:id="@+id/multiAutoCompleteTextView"  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content" />  
  21. </LinearLayout></span>  

                 程序运行效果如下图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值