步驟:
1.在布局文件中聲明Auto Complete TextView;
2.定義提示條目的樣式(在布局文件中新建一個xml文件,聲明一個TextView控件,給其設置樣式);
3.創建一個ArrayAdapter(AutoCompleteTextView需要使用ArrayAdapter提供數據);
4.為autoCompleteTextView設置數據
xml代碼:activity_main.xml
[html]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="@+id/autoComplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
item_list.xml:
[html]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/item_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
activity代碼:
[java]
package com.example.autocompletetextview1;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends Activity
{
private AutoCompleteTextView autoComplete;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autoComplete=(AutoCompleteTextView)findViewById(R.id.autoComplete);
List<String> list=new ArrayList<String>();
list.add("hellokitty");
list.add("nnnkimi");
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.item_list, R.id.item_list, list);
autoComplete.setAdapter(adapter);
}
}
默認是當輸入兩個字符的時候出現提示