首页 > 系统 > Android > 正文

Android中AutoCompleteTextView与MultiAutoCompleteTextView的用法

2019-10-24 20:36:15
字体:
来源:转载
供稿:网友

这篇文章主要介绍了Android中AutoCompleteTextView与MultiAutoCompleteTextView的用法,需要的朋友可以参考下

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

AutoCompleteTextView的使用:

首先看

支持基本的自动完成功能,适用在各种搜索功能中,并且可以根据自己的需求设置他的默认显示数据。

两个控件都可以很灵活的预置匹配的那些数据,并且可以设置输入多少值时开始匹配等等功能。

布局文件很简单,如下所示:

 

 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2. xmlns:tools="http://schemas.android.com/tools" 
  3. android:layout_width="match_parent" 
  4. android:layout_height="match_parent" 
  5. android:orientation="vertical" > 
  6. <AutoCompleteTextView 
  7. android:id="@+id/tv" 
  8. android:layout_width="fill_parent" 
  9. android:layout_height="wrap_content" /> 
  10. </LinearLayout> 

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

业务代码如下:

 

 
  1. protected void onCreate(Bundle savedInstanceState) { 
  2. super.onCreate(savedInstanceState); 
  3. setContentView(R.layout.activity_main); 
  4.  
  5. mTextView = (AutoCompleteTextView)findViewById(R.id.tv); 
  6.  
  7. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,autoStr); 
  8. mTextView.setAdapter(adapter); 

MultiAutoCompleteTextView的使用:

该控件可支持选择多个值(在多次输入的情况下),分别用分隔符分开,并且在每个值选中的时候再次输入值时会自动去匹配。

可用在发短信,发邮件时选择联系人这种类型当中。

使用时需要执行设置分隔符方法。

MultiAutoCompleteTextView的使用和AutoCompleteTextView类似,只是需要设置分隔符:

具体的使用方法为在setAdapter()方法后添加:

 

  1. mTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表