android textwatcher 获取当前控件,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 adapter = new ArrayAdapter(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軟件編程有所幫助。
總結
以上是生活随笔為你收集整理的android textwatcher 获取当前控件,Android中AutoCompleteTextView与TextWatcher结合小实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [js] js怎样避免原型链上的对象共
- 下一篇: Android PreferenceAc