1、布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:text=""
android:completionThreshold="2"
android:completionHint="请输入搜索内容"
android:layout_weight="7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:text="搜索"
android:layout_weight="1"
android:layout_marginLeft="10px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
2、在MainAcitivity中,定义一个字符串数组常量
public static final String[] CITIES=new String[]{"中国北京","中国上海","上海"};
3、在onCreate()方法中
//获取自动完成文本框
AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,CITIES);
textView.setAdapter(adapter);
4、获取按钮
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,textView.getText().toString(),Toast.LENGTH_SHORT).show();
}
});