使用AutoCompleteTextView控件的步骤

AutoCompleteTextView的功能类似于百度或者Google在搜索栏输入信息的时候,弹出的与输入信息接近的提示信息。具体效果是,一个可编辑的文本视图,当用户输入信息后弹出提示。提示列表显示在一个下拉菜单中,用户可以从中选择一项,以完成输入。提示列表是从一个数据适配器获取的数据。

以下是使用的步骤:

第一步:在布局文件中定义控件

 
 
  1. <AutoCompleteTextView  
  2.  
  3.         android:id="@+id/actv" 
  4.  
  5.         android:layout_width="fill_parent" 
  6.  
  7.         android:layout_height="wrap_content" 
  8.  
  9. /> 

第二步:在Activity中引用

 
 
  1. AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.actv); 

第三步:创建一个适配器来保存数据

 
 
  1. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this
  2.  
  3. ndroid.R.layout.simple_dropdown_item_1line,new String[] {"English""Hebrew""Hindi""German" }); 

第四步:将适配器与AutoCompleteTextView相关联

 
 
  1. actv.setAdapter(adapter); 
THE END