Android中AutoCompleteTextView與TextWatcher結合小執行個體_Android

來源:互聯網
上載者:User

AutoCompleteTextView是實現動態匹配輸入內容的一種輸入框(EditText),如輸入“and”時,會提示“Android”

效果圖:

實現代碼:

package com.conowen.test;  import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.AutoCompleteTextView;  public class DrComActivity extends Activity {    /** Called when the activity is first created. */   @Override   public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);           AutoCompleteTextView autoinput =(AutoCompleteTextView) findViewById(R.id.autoinput);      autoinput.setThreshold(1);// 輸入一個字母就開始自動提示     autoinput.addTextChangedListener(new TextWatcher() {          @Override     public void onTextChanged(CharSequence s, int start, int before, int count) {       // TODO Auto-generated method stub       //s是輸入框正在輸的字串,隨著不斷的輸入,s的值也會不停地改變                    String str = s.toString();                  String[] temp = getInputAdapter(getInputWorldOrder(str));          //此處代碼省略,自己通過查詢資料庫或者其他方法,動態地擷取相應的字串數組          //如做一個字典時,不可能預先把所有單詞做成一個adapter,應該根據輸入的字元,         //動態地查詢一定數量的相對應的單詞,然後再構建adapter          ArrayAdapter<String> adapter = new ArrayAdapter<String>(ct,          android.R.layout.simple_dropdown_item_1line, temp);               autoinput.setAdapter(adapter)           //正在輸入時,構建adapter,然後把adapter綁定在AutoCompleteTextView 上面      @Override     public void beforeTextChanged(CharSequence s, int start, int count,         int after) {       // TODO Auto-generated method stub      }      @Override     public void afterTextChanged(Editable s) {       // TODO Auto-generated method stub      }    }   } }

以上就是本文的全部內容,希望對大家學習Android軟體編程有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.