XML布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="请输入城市:"
android:completionThreshold="2"
/><!--
android:ems 指定补全框的宽度
android:completionThreshold 设置输入几个开始提示 --> <MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/autoCompleteTextView1"
android:ems="10"
android:hint="请输入收件人:"
android:completionThreshold="2"> <requestFocus />
</MultiAutoCompleteTextView> </RelativeLayout>
源代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView; public class MainActivity2 extends Activity {
private AutoCompleteTextView at;
private MultiAutoCompleteTextView mat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
at = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
mat = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
18 //定义适配器 /* context,上下文对象
objects 设置提示的数据源
resource, 设置提示显示的布局文件
*/
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.city_name, android.R.layout.simple_spinner_dropdown_item);
//设置适配器
at.setAdapter(adapter);
mat.setAdapter(adapter);
//设置分隔符
mat.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); } }