AutoCompleteTextView,有一個text作為首碼,有一個類似spinner作為選擇,使用者也可以敲入所需,可不在spinner的list之中。
AutoCompleteTextView是EditText的subclass,可以使用EditText所有的屬性,例如字型,顏色等。此外,提供android:completionThreshold,用於表明最小要敲入多少字元才開始顯示list filter。我們可以通過setAdapter設定一個adapter來給出list的內容,但是由於使用者可以自行填入內同,因此不支援selected listener,我們可以註冊一個TextWatcher,就如同其他EditText widget一樣,來檢測特系統的變化,它在使用者從list中選擇或者人工填入都可以監測到。
步驟一:編寫:Android XML
下面是XML的例子
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/selection6"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<AutoCompleteTextView android:id="@+id/editAuto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="3" />
<!-- 在我們測試中,預設為2,即我們敲入第2個字元的時候可以觸發list顯示 -->
</LinearLayout>
步驟二:JAVA source code
//步驟2.1 增加介面,實現TextWatcher
public class Chapter7Test6 extends Activity implements TextWatcher{
private TextView selection;
private AutoCompleteTextView edit;
private String[] items={"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing", "elit", "morbi", "vel", "ligula", "vitae", "arcu", "aliquet", "mollis", "etiam", "vel", "erat", "placerat", "ante", "porttitor", "sodales", "pellentesque",
"augue", "purus"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chapter_7_test6);
selection = (TextView)findViewById(R.id.selection6);
edit = (AutoCompleteTextView)findViewById(R.id .editAuto);
//步驟1:設定適配器
edit.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,items));
//步驟2:設定觸發函數,採用TextWatcher,
edit.addTextChangedListener(this);
}
/* 步驟2.2,TextWatcher將需實現afterTextChanged(),beforeTextChanged(),onTextChanged()三個方法。本例無須處理after和before,之針對onTextChanged進行處理 */
public void afterTextChanged(Editable s) {
//nothing to do
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//nothing to do
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
selection.setText(edit.getText());
}
}
來自Android Tutorial的例子:在這個例子中,我們不採用自訂的List格式,通過XML來定義List的格式
1)設定描述List中item的格式
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="16sp"
android:padding="10dp"
android:textColor="#0000aa">
</TextView>
Android長度單位詳解(dp、sp、px、in、pt、mm、dip)
android中定義的dimension單位有以下這些:
px(Pixels ,像素):對應螢幕上的實際像素點。
in(Inches ,英寸):螢幕物理長度單位。
mm(Millimeters ,毫米):螢幕物理長度單位。
pt(Points ,磅):螢幕物理長度單位,1/72英寸。
dp(與密度無關的像素):邏輯長度單位,在 160 dpi 螢幕上,1dp=1px=1/160英寸。隨著密度變化,對應的像素數量也變化,但並沒有直接的變化比例。
dip:與dp相同,多用於Google樣本中。
sp(與密度和字型縮放度無關的像素):與dp類似,但是可以根據使用者的字型大小喜好設定進行縮放。
2)設定Activity中的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView android:text="Country"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<AutoCompleteTextView android:id="@+id/auto_country"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:completionThreshold="1" />
</LinearLayout>
3)原始碼:這裡我們不進行觸發,僅顯示內容
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.auto_country);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.chapter_7_test7_item,Chapter7Test2.COUNTRIES);
textView.setAdapter(adapter);
相關連結:
我的Android開發相關文章