標籤:
AutoCompleteTextView
1.功能:
動態匹配輸入的內容,如百度搜尋引擎當輸入文本時可以根據內容顯示匹配的熱門資訊。
2.獨特屬性:
android:completionThreshold 設定輸入多少字元時自動匹配
使用AutoCOmpleteTextView實現自動匹配輸入的內容
private AutoCompleteTextView acTextView;
初始化控制項
acTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
初始化資料來源(代碼裡res數組又增加了一些)
private String[] res = new String[] { "moon", "month", "moonlight", "moonlight poet" };
建立一個適配器(這裡使用ArrayAdapter)
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, res);
講adapter與當前AutoCompleteTextView綁定(通過.setAdapter()方法綁定)
acTextView.setAdapter(adapter);
在activity_main.xml設定當輸入多少字元時出現adapter中的內容
android:completionThreshold="1"
MultiAutoCompleteTextView
1.功能
可支援選擇多個值(在多次輸入的情況下),分別用分隔字元分開,並且在每個值選中的時候再次輸入值會自動去匹配,可用在可簡訊,發郵件時選擇連絡人這種類型當中。
2.獨特屬性:
android:completionThreshold 設定輸入多少字元時自動匹配
3.設定分隔字元
mtxt.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
使用MultiAutoCOmpleteTextView實現自動匹配輸入的內容
具體步驟和AutoCompleteTextView類似。
設定分隔字元(設定以逗號為分隔字元)
macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <AutoCompleteTextView android:id="@+id/autoCompleteTextView1" android:completionThreshold="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入你要搜尋的關鍵詞" > <requestFocus /> </AutoCompleteTextView> <MultiAutoCompleteTextView android:id="@+id/multiAutoCompleteTextView1" android:completionThreshold="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="請輸入關鍵詞(可多個,逗號分隔)" /> </LinearLayout>
activity_main.xml
package com.example.autocompletetextviewandmulti;import android.support.v7.app.ActionBarActivity;import android.support.v7.app.ActionBar;import android.support.v4.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;import android.widget.MultiAutoCompleteTextView;import android.os.Build;public class MainActivity extends ActionBarActivity { private AutoCompleteTextView acTextView; private MultiAutoCompleteTextView macTextView; private String[] res = new String[] { "apple", "banana", "China", "delicious", "eight", "fight", "good", "hello", "intelligence", "just", "kilometer", "like", "moon", "month", "moonlight", "moonlight poet", "night", "ok", "pear", "queue", "ring", "stack", "tail", "unique", "very", "week", "x-Men", "yellow", "zero" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); acTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, res); acTextView.setAdapter(adapter); macTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1); macTextView.setAdapter(adapter); macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); }}MainActivity.java
效果:
Android 實現動態匹配輸入的內容 AutoCompleteTextView和MultiAutoCompleteTextView