使用autoCompleteTextView以及MultiAutoCompleteTextView实现自动匹配输入内容
一:autoCompleteTextView
1:控件属性设置:
注意添加:android:completionThreshold="1"来设置输入几个字符的时候开始显示匹配的内容
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > //completionThreshould 定义输入几个字符的时候开始匹配 <AutoCompleteTextView android:completionThreshold="1" android:id="@+id/autoCompleteTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:hint="@string/name" > <requestFocus /> </AutoCompleteTextView> </RelativeLayout>
2:MainActive文件设置
package com.example.demo2; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class MainActivity extends Activity { private AutoCompleteTextView acTv; private String[] res = {"北京1", "北京2", "北京3", "上海", "上徐", "上海2"}; @Override /* * 1: 新建一个适配器 * 2: 初始化数据源,用这里的数据源去匹配文本框输入的内容 * 3: 将adpter与当前AutoCompleteTextView类的对象绑定 * @see android.app.Activity#onCreate(android.os.Bundle) */ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //根据id查找AutoCompleteTextView对象 acTv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); //然后创建一个适配器用于匹配 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, res); acTv.setAdapter(adapter);//将adapter这个适配器与AutoCompleteTextView对象(控件)绑定 } }
2:MultiAutoCompleteTextView使用
与AotoCompleteTextView不同的就是他可以在同一个输入框中实现多个匹配
需要多设置一个分隔符,macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
其他同上
主文件:
package com.example.demo2; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.MultiAutoCompleteTextView; public class MainActivity extends Activity { private AutoCompleteTextView acTv; private MultiAutoCompleteTextView macTextView; private String[] res = {"北京1", "北京2", "北京3", "上海", "上徐", "上海2"}; @Override /* * 1: 新建一个适配器 * 2: 初始化数据源,用这里的数据源去匹配文本框输入的内容 * 3: 将adpter与当前AutoCompleteTextView类的对象绑定 * @see android.app.Activity#onCreate(android.os.Bundle) */ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //根据id查找AutoCompleteTextView对象 acTv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); //然后创建一个适配器用于匹配 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, res); acTv.setAdapter(adapter);//将adapter这个适配器与AutoCompleteTextView对象(控件)绑定 macTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1); //把 MultiAutoCompleteTextView和适配器绑定 macTextView.setAdapter(adapter); //设置以逗号分隔符为结束的符号 macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); } }
2:控件属性文件
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > //completionThreshould 定义输入几个字符的时候开始匹配 <AutoCompleteTextView android:completionThreshold="1" android:id="@+id/autoCompleteTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:hint="@string/name" > <requestFocus /> </AutoCompleteTextView> <MultiAutoCompleteTextView android:completionThreshold="1" android:id="@+id/multiAutoCompleteTextView1" android:layout_margin="10dp" android:layout_below="@id/autoCompleteTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/name" /> </RelativeLayout>