Android AutoCompleteTextView控件使用
AutoCompleteTextView是一個可編輯的文本視圖顯示自動完成建議當用戶鍵入。建議列表顯示在一個下拉菜單,用戶可以從中選擇一項,以完成輸入。建議列表是從一個數據適配器獲取的數據。它有三個重要的方法clearListSelection():清除選中的列表項、dismissDropDown():如果存在關閉下拉菜單、getAdapter():獲取適配器
completionThreshold:它的值決定了你在AutoCompleteTextView至少輸入幾個字符,它才會具有自動提示的功能。另,默認最多提示20條。
dropDownAnchor:它的值是一個View的ID,指定后,AutoCompleteTextView會在這個View下彈出自動提示。
dropDownSelector:應該是設置自動提示的背景色之類的,沒有嘗試過,有待進一步考證。
dropDownWidth:設置自動提示列表的寬度。
案例代碼
1.布局文件autocompletetextview.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" > <AutoCompleteTextView android:id="@+id/antoCom_btn_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:completionHint="輸入補全提示標題" android:completionThreshold="1" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/autoText_text" /> </LinearLayout>2、java文件AutoCompleteTextViewDemo.java
package com.dream.app.start.autocompletetextview; import com.dream.app.start.R; public class AutoCompleteTextViewDemo extends PublicClass { String [] str = {"abc","abcd","abd","asd","asw","wse","wsq"}; //定義數組 // String[] province = getResources().getStringArray(R.array.province); private AutoCompleteTextView myauto = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.autocomplete); //定義數組 // String[] province = getResources().getStringArray(R.array.province); //定義數組適配器 ArrayAdapter<String> autoStr = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,str); //找到自動完成組件 myauto = (AutoCompleteTextView)findViewById(R.id.antoCom_btn_id); //為其設置適配器 myauto.setAdapter(autoStr); } }3.執行效果:
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!