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