Android 自動(dòng)補(bǔ)全提示輸入AutoCompleteTextView、 MultiAutoCompleteTextView
以在搜索框搜索時(shí),自動(dòng)補(bǔ)全為例:
其中還涉及到一個(gè)詞,Tokenizer:分詞器,分解器。
上效果圖:
MainActivity.java:
package com.joan.testautocomletetextview; import android.R.array; import android.os.Bundle; import android.app.Activity; import android.content.res.Resources; import android.view.Menu; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.MultiAutoCompleteTextView; import android.widget.MultiAutoCompleteTextView.Tokenizer; public class MainActivity extends Activity { AutoCompleteTextView actv; MultiAutoCompleteTextView mactv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); actv = (AutoCompleteTextView) findViewById(R.id.actv); mactv = (MultiAutoCompleteTextView) findViewById(R.id.mactv); // 取到Strings.xml中定義的數(shù)組 String[] names = this.getResources().getStringArray(R.array.names); // 適配器 // 第三個(gè)參數(shù)是數(shù)據(jù)源 // 第二個(gè)參數(shù)是樣式資源的id ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, names); // =============只能選擇單個(gè)的自動(dòng)補(bǔ)全===================== actv.setAdapter(adapter); // =============可選擇多個(gè)的自動(dòng)補(bǔ)全===================== // Tokenizer分詞器,分解器 // MultiAutoCompleteTextView.CommaTokenizer();這個(gè)簡(jiǎn)易的分解器可用于對(duì)由逗號(hào)和若干空格分割的列表進(jìn)行分解. Tokenizer t = new MultiAutoCompleteTextView.CommaTokenizer(); mactv.setAdapter(adapter); mactv.setTokenizer(t); } }
strings.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">TestAutocompleteTextView</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <!--定義一個(gè)數(shù)組 --> <string-array name="names"> <item >zhangyu</item> <item >zhangxinzhe</item> <item >zhangxingxing</item> <item >liudehua</item> <item >liuyi</item> </string-array> </resources>
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="只可選擇單個(gè)" /> <!--AutoCompleteTextView 自動(dòng)補(bǔ)全,只能選擇一個(gè)值 android:completionThreshold="1" 輸入第一個(gè)字后自動(dòng)補(bǔ)全 --> <AutoCompleteTextView android:id="@+id/actv" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請(qǐng)輸入搜索的名字" android:completionThreshold="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="可選擇多個(gè)" /> <!--MultiAutoCompleteTextView 可以選擇多個(gè)值 --> <MultiAutoCompleteTextView android:id="@+id/mactv" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請(qǐng)輸入搜索的名字" android:completionThreshold="1" /> </LinearLayout>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- 實(shí)例講解Android中的AutoCompleteTextView自動(dòng)補(bǔ)全組件
- Android實(shí)現(xiàn)登錄郵箱的自動(dòng)補(bǔ)全功能
- Android中EditText和AutoCompleteTextView設(shè)置文字選中顏色方法
- Android中AutoCompleteTextView與MultiAutoCompleteTextView的用法
- Android AutoCompleteTextView控件使用實(shí)例
- 基于Android中的 AutoCompleteTextView實(shí)現(xiàn)自動(dòng)填充
- Android AutoCompleteTextView連接數(shù)據(jù)庫(kù)自動(dòng)提示的方法(附demo源碼下載)
- Android AutoCompleteTextView自動(dòng)提示文本框?qū)嵗a
- Android仿百度谷歌搜索自動(dòng)提示框AutoCompleteTextView簡(jiǎn)單應(yīng)用示例
- Android用戶輸入自動(dòng)提示控件AutoCompleteTextView使用方法
相關(guān)文章
圖文詳解Android Studio搭建Android集成開(kāi)發(fā)環(huán)境的過(guò)程
這篇文章主要以圖文的方式詳細(xì)介紹了Android Studio搭建Android集成開(kāi)發(fā)環(huán)境的過(guò)程,文中安裝步驟介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-12-12Android入門之在子線程中調(diào)用Handler詳解
這篇文章主要為大家詳細(xì)介紹了Android如何在子線程中調(diào)用Handler,文中的示例代碼講解詳細(xì),有需要的朋友可以借鑒參考下,希望能夠?qū)Υ蠹矣兴鶐椭?/div> 2022-12-12OpenGL Shader實(shí)現(xiàn)光照發(fā)光體特效
這篇文章主要介紹了如何通過(guò)OpenGL Shader實(shí)現(xiàn)光照發(fā)光體特效,不同于陰影遮蓋,它是利用圓形繪制向內(nèi)部。感興趣的小伙伴可以了解一下2022-02-02Android簡(jiǎn)易圖片瀏覽器的實(shí)現(xiàn)
最近做了一個(gè)圖片瀏覽小程序,本文主要介紹了Android簡(jiǎn)易圖片瀏覽器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03Android實(shí)現(xiàn)橫向滑動(dòng)卡片效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)橫向滑動(dòng)卡片效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android?studio?利用共享存儲(chǔ)進(jìn)行用戶的注冊(cè)和登錄驗(yàn)證功能
這篇文章主要介紹了Android?studio?利用共享存儲(chǔ)進(jìn)行用戶的注冊(cè)和登錄驗(yàn)證功能,包括注冊(cè)頁(yè)面布局及登錄頁(yè)面功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12最新評(píng)論