Android實(shí)現(xiàn)動(dòng)態(tài)自動(dòng)匹配輸入內(nèi)容
Android實(shí)現(xiàn)動(dòng)態(tài)自動(dòng)匹配的控件主要有MultiAutoCompleteTextView和AutoCompleteTextView
MultiAutoCompleteTextView:
可支持選擇多個(gè)值(在多次輸入的情況下),分別用分隔符分開,并且在每個(gè)值選中的時(shí)候再次輸入值時(shí)會(huì)自動(dòng)去匹配
可用在發(fā)短信,發(fā)郵件時(shí)選擇聯(lián)系人這種類型當(dāng)中,使用時(shí)需要執(zhí)行設(shè)置分隔符方法.
AutoCompleteTextView:
支持基本的自動(dòng)完成功能,適用在各種搜索功能中,并且可以根據(jù)自己的需求設(shè)置他的默認(rèn)顯示數(shù)據(jù)
兩個(gè)控件都可以很靈活的預(yù)置匹配的那些數(shù)據(jù),并且可以設(shè)置輸入多少值時(shí)開始匹配等等功能
效果圖如下

輸入相應(yīng)的字符就會(huì)出現(xiàn)相應(yīng)的提示,具體操作如下
在MainActivity.java中
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
//初始化控件
private AutoCompleteTextView autoCompleteTextView;
private MultiAutoCompleteTextView multiAutoCompleteTextView;
//初始化數(shù)據(jù)源
private String [] res = {"biejing","nangchang","chengdu","shanghai"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* AutoCompleteTextView的用法
*/
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.auto_textView);
//創(chuàng)建適配器
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,res);
//將adapter與autoCompleteTextView綁定
autoCompleteTextView.setAdapter(adapter);
/**
* MultiAutoCompleteTextView的用法
*/
multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.mauto_textView);
//將adapter與multiAutoCompleteTextView綁定
multiAutoCompleteTextView.setAdapter(adapter);
//設(shè)置以逗號(hào)為分隔符結(jié)束的符號(hào)
multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
}
在activity_layout.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myapplication.MainActivity"
android:orientation="vertical"
>
<AutoCompleteTextView
android:completionThreshold="3"
android:id="@+id/auto_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入關(guān)鍵字"
/>
<MultiAutoCompleteTextView
android:hint="請(qǐng)輸入多個(gè)關(guān)鍵字"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/auto_textView"
android:id="@+id/mauto_textView" />
</RelativeLayout>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android仿優(yōu)酷圓形菜單學(xué)習(xí)筆記分享
這篇文章主要為大家分享了Android仿優(yōu)酷圓形菜單學(xué)習(xí)筆記,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
使用PHP開發(fā)Android應(yīng)用程序技術(shù)介紹
這篇文章主要介紹了使用PHP開發(fā)Android應(yīng)用程序技術(shù)介紹,本文講解了安裝PHP for Android、設(shè)置PHP for Android開發(fā)環(huán)境、使用PHP構(gòu)建Android應(yīng)用程序,需要的朋友可以參考下2015-03-03
Android實(shí)現(xiàn)在TextView文字過長(zhǎng)時(shí)省略部分或滾動(dòng)顯示的方法
這篇文章主要介紹了Android實(shí)現(xiàn)在TextView文字過長(zhǎng)時(shí)省略部分或滾動(dòng)顯示的方法,結(jié)合實(shí)例形式分析了Android中TextView控件文字顯示及滾動(dòng)效果相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
利用SurfaceView實(shí)現(xiàn)下雨與下雪動(dòng)畫效果詳解(Kotlin語法)
這篇文章主要給大家介紹了關(guān)于利用SurfaceView實(shí)現(xiàn)下雨與下雪動(dòng)畫效果的相關(guān)資料,需要一些基本的View知識(shí)和會(huì)一些基礎(chǔ)Kotlin語法,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
Android LaunchMode四種啟動(dòng)模式詳細(xì)介紹
這篇文章主要介紹了Android LaunchMode四種啟動(dòng)模式詳細(xì)介紹的相關(guān)資料,這里對(duì)launchmode的使用方法進(jìn)行了詳解及啟動(dòng)模式的比較,需要的朋友可以參考下2016-12-12

