标签:
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对象(控件)绑定 } }
使用autoCompleteTextView实现自动匹配输入内容
标签:
原文地址:http://www.cnblogs.com/rain-1/p/5146334.html