Android的AutoCompleteTextView與TextWatcher的結合
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 } } }
}</pre>
本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!