1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

     xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="com.example.top5_1.MainActivity" > <EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:background="#FFA500"
android:textSize="16sp"
android:drawableLeft="@drawable/ic_launcher"
android:text="单词搜索补全演示" />
<AutoCompleteTextView
android:id="@+id/ed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:attr/textViewStyle"
android:hint="请输入单词"
//设置从第二个字符开始提示
android:completionThreshold="2"
android:layout_marginTop="10dp" /></LinearLayout>

JAVA:

 public class MainActivity extends Activity{
private AutoCompleteTextView act;
//设置数据源
private static final String[] words={"abc","acd","ade"}; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
act=(AutoCompleteTextView) findViewById(R.id.ed);
//将数据源赋到文本框
ArrayAdapter<String> aa=new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,words);
act.setAdapter(aa);
};

Android基础TOP5_1:AutoCompleteTextView自动补全文本框-LMLPHP

05-08 15:21