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) {


}

});

}

}


你可能感兴趣的:(TextWatcher,EditText自动检索字段)