Android UI控制元件系列:AutoCompleteTextView(自動提示)
AutoCompleteTextView的功能類似於百度或者Google在搜尋欄輸入資訊的時候,彈出的與輸入資訊接近的提示資訊。當然這裡要要用到一些介面卡
在Android中提供了兩智慧輸入框,它們是MultiAutoCompleteTextView、AutoCompleteTextView。它們的功能大致一樣,它和AutoCompleteTextView的區別就是MultiAutoCompleteTextView可以在輸入框中一直增加新的選取值。編寫方式也有所不同,在進行setAdapter之後還需要呼叫setTokenizer() 。下面詳細介紹一下。
一、AutoCompleteTextView
1.簡介
一個可編輯的文字檢視,當使用者輸入資訊後彈出提示。提示列表顯示在一個下拉選單中,使用者可以從中選擇一項,以完成輸入。提示列表是從一個資料介面卡獲取的資料。
2.重要方法
clearListSelection():清除選中的列表項
dismissDropDown():如果存在關閉下拉選單
getAdapter():獲取介面卡
3.建立須知
(1)佈局檔案
<AutoCompleteTextView android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="wrap_content" />
(2)程式
例項化介面卡
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, strs);
設定介面卡
edit.setAdapter(adapter);
二、MultiAutoCompleteTextView
1.簡介
繼承自AutoCompleteTextView,延長AutoCompleteTextView的長度,你必須要提供一個MultiAutoCompleteTextView.Tokenizer來區分不同的子串
2.重要方法
enoughToFilter():當文字長度超過閾值時過濾
performValidation():代替驗證整個文字,這個子類方法驗證每個單獨的文字標記
setTokenizer(MultiAutoCompleteTextView.Tokenizer t);使用者正在輸入時,tokenizer設定將用於確定文字相關範圍內
3.使用須知
(1)佈局檔案
<MultiAutoCompleteTextView android:id="@+id/edit1" android:layout_width="match_parent" android:layout_height="wrap_content" />
(2)程式
例項化介面卡
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, strs);
設定介面卡
edit.setAdapter(adapter);
確定範圍
edit1.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer())
下面是個例子
AutoCommitTest.java
package org.hualang.auto; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.MultiAutoCompleteTextView; public class AutoCommitTest extends Activity { /** Called when the activity is first created. */ private static final String[] autoString=new String[]{"welcome","well", "weatch","weexeview","werap"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //關聯關鍵字 ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, autoString); AutoCompleteTextView autocomplete=(AutoCompleteTextView)findViewById(R.id.auto); autocomplete.setAdapter(adapter); MultiAutoCompleteTextView multi=(MultiAutoCompleteTextView) findViewById(R.id.multi); //將adapter新增到AutoCompleteTextView中 multi.setAdapter(adapter); multi.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); } }
main.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:layout_width="fill_parent" android:layout_height="wrap_content" android:text="自動提示功能演示" /> <AutoCompleteTextView android:id="@+id/auto" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <MultiAutoCompleteTextView android:id="@+id/multi" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
執行結果如下:
相關文章
- 【Android】自動提示匹配之AutoCompleteTextViewAndroidTextView
- Android UI系列-----EditText和AutoCompleteTextViewAndroidUITextView
- Android UI控制元件系列:Toast(提示)AndroidUI控制元件AST
- Android UI 設計(13):AutoCompleteTextViewAndroidUITextView
- Android UI控制元件系列:Button(按鈕)AndroidUI控制元件
- Android UI控制元件系列:TextView(文字框)AndroidUI控制元件TextView
- Android UI控制元件系列:TableLayout(表格佈局)AndroidUI控制元件
- Android UI控制元件系列:Spinner(下拉選單)AndroidUI控制元件
- Android UI控制元件系列:ProgressBar(進度條)AndroidUI控制元件
- Android UI控制元件系列:Gallery(畫廊檢視)AndroidUI控制元件
- Android UI控制元件系列:TabWidget(切換卡)AndroidUI控制元件
- Android UI控制元件系列:WebView(網路檢視)AndroidUI控制元件WebView
- 自動化測試系列 —— UI自動化測試UI
- Android UI控制元件系列:Tab Layout(選項卡布局)AndroidUI控制元件
- Android UI控制元件系列:RelativeLayout(相對佈局)AndroidUI控制元件
- Android UI控制元件系列:GridView(網格佈局)AndroidUI控制元件View
- Android UI控制元件系列:Dialog(對話方塊)AndroidUI控制元件
- Android--自動搜尋提示Android
- Android UI控制元件系列:LinearLayout(線性佈局)AndroidUI控制元件
- Android UI控制元件系列:ImageButton(帶圖示的按鈕)AndroidUI控制元件
- Android UI控制元件系列:RadioButton(單選按鈕)AndroidUI控制元件
- 自動化測試系列(三)|UI測試UI
- Android--多選自動搜尋提示Android
- android 5個自動化測試Ui框架AndroidUI框架
- Android UI控制元件系列:DatePicker,TimePicker(日期和時間選擇)AndroidUI控制元件
- Android手機QQ的UI自動化實踐AndroidUI
- Android使用Espresso進行UI自動化測試AndroidEspressoUI
- Android UI 自動化測試實現過程AndroidUI
- Android 介面(1):UI 開發控制元件AndroidUI控制元件
- Android UI 設計(4):EditText 控制元件AndroidUI控制元件
- Android高階UI系列(2)-DecorViewAndroidUIView
- Jquery 自動完成控制元件jQuery控制元件
- Android自定義控制元件 帶文字提示的SeekBarAndroid控制元件
- 在做 android 盒子中 APP 的 Ui 自動化時,無法對控制元件進行操作。。不知是什麼問題。AndroidAPPUI控制元件
- Android UI系列-----ScrollView和HorizontalScrollViewAndroidUIView
- Android UI系列-----ImageView的scaleType屬性AndroidUIView
- android 根據控制元件的ID字串利用反射獲取UI控制元件Android控制元件字串反射UI
- (Android自定義控制元件)Android自定義狀態提示圖表Android控制元件