欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)動(dòng)態(tài)自動(dòng)匹配輸入內(nèi)容功能

 更新時(shí)間:2017年06月02日 10:56:38   作者:朱衛(wèi)東  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)動(dòng)態(tài)自動(dòng)匹配輸入內(nèi)容功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

什么是動(dòng)態(tài)自動(dòng)匹配輸入內(nèi)容呢?舉個(gè)例子,當(dāng)我們?cè)诎俣鹊人阉饕娴妮斎肟蛑休斎胂胍阉鞯年P(guān)鍵詞,輸入框下面會(huì)提示很多相關(guān)聯(lián)的熱門搜索項(xiàng),效果圖如下

那在安卓中如何實(shí)現(xiàn)這種效果呢?在這里給大家推薦兩個(gè)Android的控件:

AutoCompleteTextView
MultiAutoCompleteTextView

一、AutoCompleteTextView

獨(dú)特屬性:android:completionThreshold=”2”—–設(shè)置輸入多少字符時(shí)自動(dòng)匹配

首先,我們先在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="請(qǐng)輸入你要搜索的關(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; 
 //建立一個(gè)數(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ù)源,用來(lái)匹配文本框輸入的內(nèi)容
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,res);

 //將適配器與當(dāng)前控件綁定
 acTextView.setAdapter(adapter);
 }
}

這樣代碼就完成了,因?yàn)槲覀冊(cè)贏utoCompleteTextView控件中設(shè)置了android:completionThreshold=”2”,即當(dāng)我們輸入到第2個(gè)字符時(shí)開始進(jìn)行匹配,讓我們將當(dāng)前應(yīng)用程序布置到模擬器里面看一下效果:

二、MultiAutoCompleteTextView

有時(shí)候我們?cè)谖谋究蛑行枰M(jìn)行多次輸入,比如我們?cè)诎l(fā)短信或者寫郵件的時(shí)候,往往需要多選聯(lián)系人:

在這種時(shí)候,我們就可以選擇MultiAutoCompleteTextView:

  • 支持選擇多個(gè)值(在多次輸入的情況下),分別用分隔符分開,并且在每個(gè)值選中的時(shí)候再次輸入值時(shí)會(huì)自動(dòng)去匹配
  • 獨(dú)特屬性:android:completionThreshold=”2”—–設(shè)置輸入多少字符時(shí)自動(dòng)匹配
  • 設(shè)置分隔符:macTextView.setTokenizer(newMultiAutoCompleteTextView.CommaTokenizer());

這個(gè)控件的使用方法跟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="請(qǐng)輸入收件人" />
</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;
 //建立一個(gè)數(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ù)源,用來(lái)匹配文本框輸入的內(nèi)容
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,res);

 //將適配器與當(dāng)前控件綁定
 macTextView.setAdapter(adapter);

 //設(shè)置以逗號(hào)為分隔符為結(jié)束的符號(hào)
 macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
 }
}

運(yùn)行結(jié)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論