首先是布局:
<AutoCompleteTextView
android:id="@+id/acTextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="请输入你要输入的关键字" >
<requestFocus />
</AutoCompleteTextView>
<MultiAutoCompleteTextView android:id="@+id/multiAutoCompleteTextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/acTextview" android:layout_marginTop="18dp" android:hint="请输入提示文字" />
然后就是在src中添加代码:
1 public class MainActivity extends Activity { 2 private AutoCompleteTextView acTextview; 3 private String[]res={"beijing","shenzhen","shanghai"}; 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 acTextview=(AutoCompleteTextView)findViewById(R.id.acTextview); 9 ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,res); 10 acTextview.setAdapter(adapter); 11 }
macTextview.setAdapter(adapter); macTextview.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
by:慕课网