AutoComlete TextView,即自动完成文本框,从EditText派生而出,它的本质上也是一个编辑框,比普通编辑框多了一个功能:当用户输入一定字符之后,自动完成文本框会显示下拉菜单,然用户选择是否需要点击某个菜单项
首先在xml中定义一个AutoComleteTextView
<AutoCompleteTextView android:id="@+id/auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:completionHint="请选择您喜欢的资源:" android:dropDownHorizontalOffset="10dp" android:completionThreshold="1"/>
然后继续定义一个MultiAutoComleteTextView,MultiAutoComleteTextView是AutoComleteTextView的子项,这子项允许输入多个提示项
<MultiAutoCompleteTextView android:id="@+id/mauto" android:layout_width="match_parent" android:layout_height="wrap_content" android:completionThreshold="1"/>
然后打开MainActivity
public class MainActivity extends Activity{ AutoCompleteTextView act; MultiAutoCompleteTextView mautv; String[] ts = new String[]{ "itPub", "it研究", "itPUB", "it算法" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayAdapter<String> abc = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, ts); act = (AutoCompleteTextView)findViewById(R.id.auto); act.setAdapter(abc); mautv = (MultiAutoCompleteTextView)findViewById(R.id.mauto); mautv.setAdapter(abc); mautv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); }}