Android UI控制元件系列:AutoCompleteTextView(自動提示)

apkbus發表於2014-12-04

AutoCompleteTextView的功能類似於百度或者Google在搜尋欄輸入資訊的時候,彈出的與輸入資訊接近的提示資訊。當然這裡要要用到一些介面卡

在Android中提供了兩智慧輸入框,它們是MultiAutoCompleteTextView、AutoCompleteTextView。它們的功能大致一樣,它和AutoCompleteTextView的區別就是MultiAutoCompleteTextView可以在輸入框中一直增加新的選取值。編寫方式也有所不同,在進行setAdapter之後還需要呼叫setTokenizer() 。下面詳細介紹一下。

一、AutoCompleteTextView

1.簡介

一個可編輯的文字檢視,當使用者輸入資訊後彈出提示。提示列表顯示在一個下拉選單中,使用者可以從中選擇一項,以完成輸入。提示列表是從一個資料介面卡獲取的資料。

2.重要方法

clearListSelection():清除選中的列表項
dismissDropDown():如果存在關閉下拉選單
getAdapter():獲取介面卡

3.建立須知

(1)佈局檔案

<AutoCompleteTextView
 android:id="@+id/edit"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" />

(2)程式

例項化介面卡

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, strs);

設定介面卡

edit.setAdapter(adapter);

二、MultiAutoCompleteTextView

1.簡介

繼承自AutoCompleteTextView,延長AutoCompleteTextView的長度,你必須要提供一個MultiAutoCompleteTextView.Tokenizer來區分不同的子串

2.重要方法

enoughToFilter():當文字長度超過閾值時過濾
performValidation():代替驗證整個文字,這個子類方法驗證每個單獨的文字標記
setTokenizer(MultiAutoCompleteTextView.Tokenizer t);使用者正在輸入時,tokenizer設定將用於確定文字相關範圍內

3.使用須知

(1)佈局檔案

<MultiAutoCompleteTextView
 android:id="@+id/edit1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" />

(2)程式

例項化介面卡

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, strs);

設定介面卡

edit.setAdapter(adapter);

確定範圍

edit1.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer())

下面是個例子

AutoCommitTest.java

package org.hualang.auto;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;

public class AutoCommitTest extends Activity {
    /** Called when the activity is first created. */
        private static final String[] autoString=new String[]{"welcome","well",
                "weatch","weexeview","werap"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //關聯關鍵字
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
                        android.R.layout.simple_dropdown_item_1line,
                        autoString);
        AutoCompleteTextView autocomplete=(AutoCompleteTextView)findViewById(R.id.auto);
        autocomplete.setAdapter(adapter);
        MultiAutoCompleteTextView multi=(MultiAutoCompleteTextView) findViewById(R.id.multi);
        //將adapter新增到AutoCompleteTextView中
        multi.setAdapter(adapter);
        multi.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="自動提示功能演示"
    />
<AutoCompleteTextView
        android:id="@+id/auto"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
/>
<MultiAutoCompleteTextView
        android:id="@+id/multi"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
/>
</LinearLayout>

執行結果如下:

相關文章