android脚步---自动完成文本框
自动完成文本框AutoCompleteTextView,当用户输入一定字符时,自动完成文本框会显示一个下拉菜单,供用户选择,设置一个Adapter,该Adapter中封装了AutoCompleteTextView预设的文本
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <AutoCompleteTextView android:id="@+id/auto" android:layout_width="fill_parent" android:layout_height="wrap_content" android:completionHint="请选择您喜欢的图书" android:dropDownHorizontalOffset="20dp" android:completionThreshold="1" /> </LinearLayout>
mainactivity
package com.example.autocompletetextview; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class MainActivity extends Activity { //定义字符串数组,作为提示文本 String[] books = new String[]{ "aaa", "abbb", "accc", "addd", }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建一个ArrayAdapter,封装数组 ArrayAdapter<String> aa = new ArrayAdapter<String>( this, android.R.layout.simple_dropdown_item_1line, books); AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.auto); //设置adapter actv.setAdapter(aa); } }