·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> Android开发 >> Android中AutoCompleteTextView与MultiAutoCompleteTextView的用法

Android中AutoCompleteTextView与MultiAutoCompleteTextView的用法

作者:佚名      Android开发编辑:admin      更新时间:2022-07-23

本文以实例列举了Android中AutoCompleteTextView与MultiAutoCompleteTextView的使用方法,具体使用方法如下:

首先看AutoCompleteTextView的使用:

支持基本的自动完成功能,适用在各种搜索功能中,并且可以根据自己的需求设置他的默认显示数据。
两个控件都可以很灵活的预置匹配的那些数据,并且可以设置输入多少值时开始匹配等等功能。
布局文件很简单,如下所示:

<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" >
  <AutoCompleteTextView
    android:id="@+id/tv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

这里需要说明一下layout_width不应该设置为wrap_content,否则下拉提示只能看到第一个提示,后面的内容看不到。
业务代码如下:

protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 mTextView = (AutoCompleteTextView)findViewById(R.id.tv);
 
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,autoStr);
 mTextView.setAdapter(adapter);
}

MultiAutoCompleteTextView的使用:

该控件可支持选择多个值(在多次输入的情况下),分别用分隔符分开,并且在每个值选中的时候再次输入值时会自动去匹配。
可用在发短信,发邮件时选择联系人这种类型当中。
使用时需要执行设置分隔符方法。
MultiAutoCompleteTextView的使用和AutoCompleteTextView类似,只是需要设置分隔符:
具体的使用方法为在setAdapter()方法后添加:

mTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());