main.xml
<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:id="@+id/mtv1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <MultiAutoCompleteTextView android:id="@+id/mtv2" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> |
MainActivity.java
package com.example.autocompletetest;
import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.MultiAutoCompleteTextView; public class MainActivity extends Activity { private AutoCompleteTextView mtv1; private MultiAutoCompleteTextView mtv2; private static final String[]table = {"a", "ab", "abc", "abcd", "nice", "now", "to", "meet", "you"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mtv1 = (AutoCompleteTextView)findViewById(R.id.mtv1); mtv2 = (MultiAutoCompleteTextView)findViewById(R.id.mtv2); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, table); //adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); mtv1.setAdapter(adapter); mtv2.setAdapter(adapter); mtv2.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());//note } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } |
由于补全的字体颜色和背景颜色都是白,所以输入时只能够看到一栏一栏的。点击之后可以看到具体的提示内容。