安卓前端开发学习--5监听用户输入的内容.

来源:互联网 发布:苹果网络恢复出厂设置 编辑:程序博客网 时间:2024/05/29 16:09

有这样一个场景,一个搜索框,只要用户输入了内容就去请求服务器,于是我们在Activity里面监听EditeText文本改变事件。

EditText etOne= (EditText) findViewById(R.id.et_phone);etOne.addTextChangedListener(new TextWatcher() {            @Override            public void beforeTextChanged(CharSequence s, int start, int count, int after) {                Log.i("Ansen","内容改变之前调用:"+s);            }            @Override            public void onTextChanged(CharSequence s, int start, int before, int count) {                Log.i("Ansen","内容改变,可以去告诉服务器:"+s);            }            @Override            public void afterTextChanged(Editable s) {                Log.i("Ansen","内容改变之后调用:"+s);            } });

首先我们通过id找到EditText控件,并且添加监听函数,内部内实现TextWatcher接口,重写三个方法。我们可以在onTextChanged方法中告诉服务器我要搜索的内容。

原创粉丝点击