Android實(shí)現(xiàn)動態(tài)自動匹配輸入內(nèi)容功能
什么是動態(tài)自動匹配輸入內(nèi)容呢?舉個例子,當(dāng)我們在百度等搜索引擎的輸入框中輸入想要搜索的關(guān)鍵詞,輸入框下面會提示很多相關(guān)聯(lián)的熱門搜索項(xiàng),效果圖如下

那在安卓中如何實(shí)現(xiàn)這種效果呢?在這里給大家推薦兩個Android的控件:
AutoCompleteTextView
MultiAutoCompleteTextView
一、AutoCompleteTextView
獨(dú)特屬性:android:completionThreshold=”2”—–設(shè)置輸入多少字符時自動匹配
首先,我們先在res文件夾(我用的是AndroidStudio)下的active_main.xml下面加入AutoCompleteTextView控件,并設(shè)置好大小寬高等其他一些基礎(chǔ)屬性
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.admin.demo.MainActivity"> <AutoCompleteTextView android:completionThreshold="2" android:id="@+id/autoCompleteTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入你要搜索的關(guān)鍵詞" /> </LinearLayout>
接著,我們到j(luò)ava目錄下的MainActivity.java加入相應(yīng)的代碼:
package com.example.admin.demo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
private AutoCompleteTextView acTextView;
//建立一個數(shù)組,保存我們想要提示的文本內(nèi)容
private String[] res = {"ab1","ab2","ab3"}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件,返回類型view強(qiáng)制轉(zhuǎn)換成AutoCompleteTextView
acTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
//添加適配器,并初始化數(shù)據(jù)源,用來匹配文本框輸入的內(nèi)容
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,res);
//將適配器與當(dāng)前控件綁定
acTextView.setAdapter(adapter);
}
}
這樣代碼就完成了,因?yàn)槲覀冊贏utoCompleteTextView控件中設(shè)置了android:completionThreshold=”2”,即當(dāng)我們輸入到第2個字符時開始進(jìn)行匹配,讓我們將當(dāng)前應(yīng)用程序布置到模擬器里面看一下效果:

二、MultiAutoCompleteTextView
有時候我們在文本框中需要進(jìn)行多次輸入,比如我們在發(fā)短信或者寫郵件的時候,往往需要多選聯(lián)系人:

在這種時候,我們就可以選擇MultiAutoCompleteTextView:
- 支持選擇多個值(在多次輸入的情況下),分別用分隔符分開,并且在每個值選中的時候再次輸入值時會自動去匹配
- 獨(dú)特屬性:android:completionThreshold=”2”—–設(shè)置輸入多少字符時自動匹配
- 設(shè)置分隔符:macTextView.setTokenizer(newMultiAutoCompleteTextView.CommaTokenizer());
這個控件的使用方法跟AutoCompleteTextView大體上還是差不多的,只是多了設(shè)置分隔符這一步
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.admin.demo.MainActivity"> <MultiAutoCompleteTextView android:completionThreshold="2" android:id="@+id/multiAutoCompleteTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入收件人" /> </LinearLayout>
MainActivity.java:
package com.example.admin.demo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
private MultiAutoCompleteTextView macTextView;
//建立一個數(shù)組,保存我們想要提示的文本內(nèi)容
private String[] res = {"ab1","ab2","ab3","cd1","cd2","cd3"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件,返回類型view強(qiáng)制轉(zhuǎn)換成AutoCompleteTextView
macTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView);
//添加適配器,并初始化數(shù)據(jù)源,用來匹配文本框輸入的內(nèi)容
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,res);
//將適配器與當(dāng)前控件綁定
macTextView.setAdapter(adapter);
//設(shè)置以逗號為分隔符為結(jié)束的符號
macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
}
運(yùn)行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android垂直切換的圓角Banner與垂直指示器相關(guān)介紹與應(yīng)用詳解
這篇文章主要介紹了Android垂直切換的圓角Banner與垂直指示器相關(guān)介紹與應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10
ubuntu用wifi連接android調(diào)試程序的步驟
這篇文章主要介紹了ubuntu用wifi連接android調(diào)試程序的步驟,需要的朋友可以參考下2014-02-02
實(shí)例詳解Android文件存儲數(shù)據(jù)方式
總體的來講,數(shù)據(jù)存儲方式有三種:一個是文件,一個是數(shù)據(jù)庫,另一個則是網(wǎng)絡(luò)。下面通過本文給大家介紹Android文件存儲數(shù)據(jù)方式,對android文件存儲數(shù)據(jù)相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-01-01
安卓開發(fā)之FragmentPagerAdapter和FragmentStatePagerAdapter詳解
這篇文章主要介紹了安卓開發(fā)之FragmentPagerAdapter和FragmentStatePagerAdapter詳解的相關(guān)資料,需要的朋友可以參考下2022-08-08
Jetpack Compose實(shí)現(xiàn)動畫效果的方法詳解
compose為支持動畫提供了大量的 api,通過這些 api 我們可以輕松實(shí)現(xiàn)動畫效果。本文將為大家介紹利用compose實(shí)現(xiàn)的多種動畫效果的示例代碼,需要的可以參考一下2022-02-02
Android中使用開源框架eventbus3.0實(shí)現(xiàn)fragment之間的通信交互
本文主要介紹了Android中使用開源框架eventbus3.0實(shí)現(xiàn)fragment之間的通信交互的方法,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02
Android Studio3.6.3 當(dāng)前最新版本數(shù)據(jù)庫查找與導(dǎo)出方法(圖文詳解)
這篇文章主要介紹了Android Studio3.6.3 當(dāng)前最新版本數(shù)據(jù)庫查找與導(dǎo)出方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Android recyclerview實(shí)現(xiàn)拖拽排序和側(cè)滑刪除
這篇文章主要為大家詳細(xì)介紹了Android recyclerview實(shí)現(xiàn)拖拽排序和側(cè)滑刪除,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02

