前言:edittext的addTextChangedListener监听事件用于监听edittext的输入文本的变化,他都用于密码框,或者那种检测用户输入过程中的变化。
1.使用方式
①为edittext添加监听器
1 mEtPassword = (EditText) findViewById(R.id.id_et_password); 2 //添加监听器 3 mEtPassword.addTextChangedListener(new MyWatcher());
②创建一个实现TextWatcher接口的类,并实现他的方法
1 class MyWatcher implements TextWatcher { 2 3 // 4 public void beforeTextChanged(CharSequence s, int start, int count, 5 int after) { 6 // TODO Auto-generated method stub 7 8 } 9 10 @Override 11 public void onTextChanged(CharSequence s, int start, int before, 12 int count) { 13 // TODO Auto-generated method stub 14 15 } 16 17 18 public void afterTextChanged(Editable edit) { 19 20 } 21 }
2.实现方法的解释
<1> public void afterTextChanged(Editable edit) //检测文本改变之后的状态
edit // edit 输入结束呈现在输入框中的信息
我以log的方式进行输出,我在editext中输出文本是woshizhi
01-17 09:51:53.585: E/MainActivity(2134): edit=w 01-17 09:51:54.009: E/MainActivity(2134): edit=wo 01-17 09:51:54.033: E/MainActivity(2134): edit=wos 01-17 09:51:54.033: E/MainActivity(2134): edit=wosh 01-17 09:51:54.085: E/MainActivity(2134): edit=woshi 01-17 09:52:01.497: E/MainActivity(2134): edit=woshiy 01-17 09:52:01.517: E/MainActivity(2134): edit=woshiyi 01-17 09:52:01.697: E/MainActivity(2134): edit=woshiyiz 01-17 09:52:01.785: E/MainActivity(2134): edit=woshiyizh 01-17 09:52:01.873: E/MainActivity(2134): edit=woshiyizhi
通过上面可以看出在edittext中每输入一个词,就会相应的出来一个词
<2>
public
void
beforeTextChanged(CharSequence s,
int
start,
int
count,
int
after)//文本在改变之前的状态
//s输入框中改变前的字符串信息
//start 输入框中改变前的字符串的起始位置
//count 输入框中改变前后的字符串改变数量一般为0
//after 输入框中改变后的字符串与起始位置的偏移量
我以log的方式打印,我在edittext中同样输入woshiyizhi
01-17 10:08:41.269: E/MainActivity(2725): s=:start=0:count=0:after=1 01-17 10:09:07.197: E/MainActivity(2725): s=w:start=1:count=0:after=1 01-17 10:09:14.097: E/MainActivity(2725): s=wo:start=2:count=0:after=1 01-17 10:09:21.769: E/MainActivity(2725): s=wos:start=3:count=0:after=1 01-17 10:09:22.297: E/MainActivity(2725): s=wosh:start=4:count=0:after=1 01-17 10:09:24.117: E/MainActivity(2725): s=woshi:start=5:count=0:after=1 01-17 10:09:24.205: E/MainActivity(2725): s=woshiy:start=6:count=0:after=1 01-17 10:09:25.485: E/MainActivity(2725): s=woshiyi:start=7:count=0:after=1 01-17 10:09:25.657: E/MainActivity(2725): s=woshiyiz:start=8:count=0:after=1 01-17 10:09:25.729: E/MainActivity(2725): s=woshiyizh:start=9:count=0:after=1
通过log可以看出只有text和start参数在变化
<3>public void onTextChanged(CharSequence s, int start, int before,int count)//文本变化
//s 输入框中改变后的字符串信息
//start 输入框中改变后的字符串的起始位置
//before 输入框中改变前的字符串的位置 默认为0
//count 输入框中改变后的一共输入字符串的数量
我以log的方式打印,我在edittext中同样输入woshiyizhi
01-17 10:15:30.589: E/MainActivity(3065): s=w:start=0:after=0:count=1 01-17 10:15:46.345: E/MainActivity(3065): s=wo:start=1:after=0:count=1 01-17 10:15:48.893: E/MainActivity(3065): s=wos:start=2:after=0:count=1 01-17 10:15:49.921: E/MainActivity(3065): s=wosh:start=3:after=0:count=1 01-17 10:15:51.145: E/MainActivity(3065): s=woshi:start=4:after=0:count=1 01-17 10:15:51.681: E/MainActivity(3065): s=woshiy:start=5:after=0:count=1 01-17 10:15:52.333: E/MainActivity(3065): s=woshiyi:start=6:after=0:count=1 01-17 10:15:52.753: E/MainActivity(3065): s=woshiyiz:start=7:after=0:count=1 01-17 10:15:52.981: E/MainActivity(3065): s=woshiyizh:start=8:after=0:count=1 01-17 10:15:53.645: E/MainActivity(3065): s=woshiyizhi:start=9:after=0:count=1