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>
執行結果如下:
相關文章
- 自動化測試系列 —— UI自動化測試UI
- android 5個自動化測試Ui框架AndroidUI框架
- 自動化測試系列(三)|UI測試UI
- Android手機QQ的UI自動化實踐AndroidUI
- Android自定義控制元件 帶文字提示的SeekBarAndroid控制元件
- Android - 控制元件抖動效果Android控制元件
- 在做 android 盒子中 APP 的 Ui 自動化時,無法對控制元件進行操作。。不知是什麼問題。AndroidAPPUI控制元件
- android基礎學習-android篇day12-UI基礎控制元件(上)AndroidUI控制元件
- android基礎學習-android篇day13-UI基礎控制元件(下)AndroidUI控制元件
- nodejs 自動程式碼提示NodeJS
- 介面自動化與ui自動化區別UI
- android: 一次自繪控制元件的體驗Android控制元件
- android之豎直滾動控制元件-ListViewAndroid控制元件View
- Android撩妹特效系列!仿instagram文字自動排版功能實現!Android特效
- UI 自動化框架 yaml 大法UI框架YAML
- What?JMeter做UI自動化!JMeterUI
- 解放雙手 - Android 開發應該嘗試的 UI 自動化測試AndroidUI
- TextView、TextView的子類之EditText、EditText的子類之自動完成文字框(AutoCompleteTextView)的功能與用法TextView
- Android與Flutter混合開發-UI互動AndroidFlutterUI
- Masonry佈局控制元件,自動換行控制元件
- 全副武裝!Android UI 自動化測試在 RxImagePicker 中的實踐歷程AndroidUI
- 2018-11-19ui自動化UI
- UI 自動化測試平臺UI
- web UI 自動化工具katalon使用WebUI
- UI自動化基礎知識UI
- UI自動化測試之AirtestUIAI
- UI自動化測試實戰UI
- Android uiautomator自動化工具AndroidUI
- Android列表控制元件Android控制元件
- Android 分享控制元件Android控制元件
- android ios UIAndroidiOSUI
- Android影片編輯SDK--RDVECore來自銳動的無UI,高度抽象化APIAndroidUI抽象API
- 總結下 ui 自動化驅動架構UI架構
- android基礎學習-android篇day14-UI基礎控制元件綜合案例——點餐系統AndroidUI控制元件
- jenkins介面、UI自動化持續整合JenkinsUI
- 淺談倉儲UI自動化之路UI
- UI自動化測試工程實踐UI
- UI自動化測試框架Cypress初探UI框架
- Postman實現UI自動化測試PostmanUI