温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

EditText自动检索字段,TextWatcher

发布时间:2020-09-27 15:50:30 来源:网络 阅读:480 作者:清甘茶 栏目:开发技术

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


}

});

}

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI