使用AutoCompleteTextView和MultiAutoCompleteTextView实现输入自动匹配

1. AutoCompleteTextView

AutoCompleteTextView的布局属性和一般的TextView 并没有很大的区别,仅仅是增加了一条属性:

<AutoCompleteTextView 
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:completionThreshold="3"
        android:hint="Please enter your keywords" />
可以看到,上面的代码中多了一条completionThreshold 属性,这个是用来设置进行匹配的最小字符数的。

下来,要想实现自动匹配,需要在MainActivity中完成以下几步:

1. 获取AutoCompleteTextView 实例

2. 获取适配器

3. 将适配器与实例进行绑定

代码如下:

acTextView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
				android.R.layout.simple_list_item_1, src);
		acTextView.setAdapter(adapter);

acTextView 是AutoCompleteTextView的实例,src是需要进行匹配的数据源,这里可以是一个预先定义的数组。


2. MultiAutoCompleteTextView

MultiAutoCompleteTextView 可以用分隔符分隔,进行多次匹配。它的使用方法和AutoCompleteTextView基本相同,只不过需要设置分隔符:

<MultiAutoCompleteTextView
        android:id="@+id/multiAutoCompleteTextView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Please enter e-mail address" />

macTextView = (MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView1);
		macTextView.setAdapter(adapter);
		macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

setTokenizer用于设置分隔符,此处设置逗号为分隔符。


另外还发现如果不设置completionThreshold,默认值为2

而且关键词的匹配是不区分大小写的。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值