MainActivity如下:
package cc.testautocompletetextview; import cc.testautocompletetextview1.R; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.app.Activity; /** * Demo描述 * AutoCompleteTextView基本使用完整示例 * * 注意参数: * android:completionThreshold="2" * * 参考资料: * 《疯狂Android讲义》 作者李刚 * Thank you very much */ public class MainActivity extends Activity { private AutoCompleteTextView mAutoCompleteTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mAutoCompleteTextView=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView); String [] emails=new String []{"111@126.com","111@sina.com","111@sohu.cn","111@qq.com"}; ArrayAdapter<String> arrayAdapter =new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, emails); mAutoCompleteTextView.setAdapter(arrayAdapter); } }
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="AutoCompleteTextView使用示例" android:layout_centerHorizontal="true"/> <AutoCompleteTextView android:id="@+id/autoCompleteTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="30dip" android:completionHint="请选择:" android:dropDownVerticalOffset="2dip" android:dropDownHorizontalOffset="2dip" android:completionThreshold="2" /> </RelativeLayout>