Android实现动态自动匹配输入的内容

2020-10-30 14:39:47 浏览数 (1)

本文实例为大家分享了Android实现动态自动匹配输入内容的具体代码,供大家参考,具体内容如下

用这两个控件

分别实现这两个:

代码语言:javascript复制
package com.example.autocomplete;
 
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
 
public class MainActivity extends Activity {
 private AutoCompleteTextView acTextView;
 private String[] res = {"xxz1","xxz2","xxz3","shanghai1","shanghai2"};
 
 
 private MultiAutoCompleteTextView MulacTextView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    acTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
    
    //需要适配器
    ArrayAdapter<String  adapter = new ArrayAdapter<String (this,android.R.layout.simple_list_item_1,res);
    //初始数据源,去匹配文本框中输入的内容,然后绑定
    acTextView.setAdapter(adapter);
    
    MulacTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
    MulacTextView.setAdapter(adapter);
    //设置以逗号为分隔符结束的符号
    MulacTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    
   
}

布局文件:

代码语言:javascript复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"  
 
  <AutoCompleteTextView
    android:hint="请输入搜索的关键词"
    android:completionThreshold="3"
    android:id="@ id/autoCompleteTextView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
      
 
  </AutoCompleteTextView 
 
  <MultiAutoCompleteTextView
    android:hint="请输入搜索的邮件关键词"
    android:id="@ id/multiAutoCompleteTextView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"  
    
  </MultiAutoCompleteTextView 
 
  <CheckBox
    android:id="@ id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CheckBox" / 
 
</LinearLayout 

以上就是本文的全部内容,希望对大家的学习有所帮助。

0 人点赞