码迷,mamicode.com
首页 > 其他好文 > 详细

EditText自动检索字段,TextWatcher

时间:2015-04-29 07:21:13      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:edittext自动检索字段   textwatcher   

public class MainActivity extends Activity {


private EditText edit;

private ListView list;

private String[] str = new String[] { "1232", "45", "4631", "123485",

"87894", "4156", "1564", "5465", "45612", "98", "1247", "59789",

"7897862", "578631", "48876", "215", "2148", "7985" };

ArrayAdapter adapter;


@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.item);

edit = (EditText) findViewById(R.id.edit);

list = (ListView) findViewById(R.id.list);

adapter = new ArrayAdapter(MainActivity.this, R.layout.window,

R.id.text, str);

list.setAdapter(adapter);

edit.setOnKeyListener(new OnKeyListener() {

@Override

public boolean onKey(View arg0, int keyCode, KeyEvent KeyEvent) {

if (keyCode == KeyEvent.KEYCODE_DEL) {

/**

* 每次删除即使更新list列表

* */

EditText edit = (EditText) arg0;

adapter.getFilter().filter(edit.getText().toString());

adapter.notifyDataSetChanged();

}

return false;

}


});

edit.addTextChangedListener(new TextWatcher() {


@Override

public void afterTextChanged(Editable s) {

// String str_new = s.toString();

/**

* 利用过滤字段进行过滤掉制定的字符串

* */

adapter.getFilter().filter(s);

adapter.notifyDataSetChanged();

}


@Override

public void beforeTextChanged(CharSequence s, int start, int end,

int count) {


}


@Override

public void onTextChanged(CharSequence s, int start, int end,

int count) {


}

});

}

}


本文出自 “清甘茶” 博客,请务必保留此出处http://shunshuncon.blog.51cto.com/8232441/1640116

EditText自动检索字段,TextWatcher

标签:edittext自动检索字段   textwatcher   

原文地址:http://shunshuncon.blog.51cto.com/8232441/1640116

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!