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í)或者工作能帶來一定的幫助,同時(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開發(fā)之電話撥號(hào)器和短信發(fā)送器實(shí)現(xiàn)方法
這篇文章主要介紹了Android開發(fā)之電話撥號(hào)器和短信發(fā)送器實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android電話撥號(hào)器和短信發(fā)送器的具體原理與實(shí)現(xiàn)步驟,需要的朋友可以參考下2015-12-12
android用java動(dòng)態(tài)增添刪除修改布局
這篇文章主要介紹了android用java動(dòng)態(tài)增添刪除修改布局,感興趣的小伙伴們可以參考一下2016-03-03
Android性能優(yōu)化之線程監(jiān)控與線程統(tǒng)一詳解
這篇文章主要為大家介紹了Android性能優(yōu)化之線程監(jiān)控與線程統(tǒng)一詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android二級(jí)緩存加載圖片實(shí)現(xiàn)照片墻功能
這篇文章主要為大家詳細(xì)介紹了Android二級(jí)緩存加載圖片實(shí)現(xiàn)照片墻功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
android實(shí)現(xiàn)ViewPager懶加載的三種方法
這篇文章主要介紹了android實(shí)現(xiàn)ViewPager懶加載的三種方法,懶加載在項(xiàng)目運(yùn)用中很廣泛,可以提高運(yùn)行速度,有興趣的可以了解一下。2017-03-03
Android 之BottomsheetDialogFragment仿抖音評(píng)論底部彈出對(duì)話框效果(實(shí)例代碼)
這篇文章主要介紹了Android 中之BottomsheetDialogFragment仿抖音評(píng)論底部彈出對(duì)話框效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Android開發(fā)實(shí)現(xiàn)的ViewPager引導(dǎo)頁功能(動(dòng)態(tài)加載指示器)詳解
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)的ViewPager引導(dǎo)頁功能(動(dòng)態(tài)加載指示器),結(jié)合實(shí)例形式詳細(xì)分析了Android使用ViewPager引導(dǎo)頁的具體步驟,相關(guān)布局、功能使用技巧,需要的朋友可以參考下2017-11-11
Android程序開發(fā)之使用Design包實(shí)現(xiàn)QQ動(dòng)畫側(cè)滑效果和滑動(dòng)菜單導(dǎo)航
這篇文章主要介紹了Android程序開發(fā)之使用Design包實(shí)現(xiàn)QQ動(dòng)畫側(cè)滑效果和滑動(dòng)菜單導(dǎo)航的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Android開發(fā)中使用Volley庫(kù)發(fā)送HTTP請(qǐng)求的實(shí)例教程
這篇文章主要介紹了Android開發(fā)中使用Volley庫(kù)發(fā)送HTTP請(qǐng)求的實(shí)例教程,包括創(chuàng)建Volley單例的基本知識(shí)與取消Request請(qǐng)求的技巧等,需要的朋友可以參考下2016-05-05

