Android 實現動態匹配輸入的內容 AutoCompleteTextView和MultiAutoCompleteTextView

來源:互聯網
上載者:User

標籤:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.