Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自動補全輸入:AutoCompleteTextView 與 MultiAutoCompleteTextView

編輯:關於 Android 程式設計

搜尋框在輸入時即時提示候選字,靠的就是 AutoCompleteTextView;若要一次輸入多筆(例如寄信收件人),則用 MultiAutoCompleteTextView。這裡還牽涉到一個關鍵概念——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中定義的數組 String[] names = this.getResources().getStringArray(R.array.names); // 適配器 // 第三個參數是數據源 // 第二個參數是樣式資源的id ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, names); // =============只能選擇單個的自動補全===================== actv.setAdapter(adapter); // =============可選擇多個的自動補全===================== // Tokenizer分詞器,分解器 // MultiAutoCompleteTextView.CommaTokenizer();這個簡易的分解器可用於對由逗號和若干空格分割的列表進行分解. 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> <!--定義一個數組 --> <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="https://schemas.android.com/apk/res/android" xmlns:tools="https://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="只可選擇單個" /> <!--AutoCompleteTextView 自動補全,只能選擇一個值 android:completionThreshold="1" 輸入第一個字後自動補全 --> <AutoCompleteTextView android:id="@+id/actv" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入搜索的名字" android:completionThreshold="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="可選擇多個" /> <!--MultiAutoCompleteTextView 可以選擇多個值 --> <MultiAutoCompleteTextView android:id="@+id/mactv" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入搜索的名字" android:completionThreshold="1" /> </LinearLayout>

幾個實務重點:completionThreshold="1" 表示輸入一個字就開始提示,設為 2 則需輸入兩個字,能大幅減少高頻字的噪音提示。而 CommaTokenizer 的作用正是告訴 MultiAutoCompleteTextView「遇到逗號就算一個詞結束」,因此使用者可以在同一個輸入框中連續輸入多個人名,每輸入一段逗號後再打下一個就會重新觸發補全。若要搭配自訂樣式的提示清單,則需自行撰寫 Adapter 並提供自訂的 dropdown 版面。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved