我使用TextWatcher,因为我想动态获取字符串。
我在EditText中输入qw。然后我做退格以获得零长度字符串。问题是,当我离开q时,只调用before方法。知道为什么吗?
03-05 11:19:10.549 22390-22390/com.narb.check I/TRT changed: q
03-05 11:19:10.589 22390-22390/com.narb.check I/TRT after: q
03-05 11:19:19.359 22390-22390/com.narb.check I/TRT before: q
03-05 11:19:19.359 22390-22390/com.narb.check I/TRT changed: qw
03-05 11:19:19.359 22390-22390/com.narb.check I/TRT after: qw
first backspace
03-05 11:19:48.679 22390-22390/com.narb.check I/TRT before: qw
03-05 11:19:48.679 22390-22390/com.narb.check I/TRT changed: q
03-05 11:19:48.679 22390-22390/com.narb.check I/TRT after: q
second backspace (to get a null string)
03-05 11:20:41.379 22390-22390/com.narb.check I/TRT before: q
这是我的代码:
edt.addTextChangedListener(new TextWatcher()
{
@Override
public void afterTextChanged(Editable mEdit)
{
ip_host = mEdit.toString();
Log.i("TRT after",mEdit.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after){
Log.i("TRT before",s.toString());
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
Log.i("TRT changed",s.toString());
}
});
和我的EditText:
<EditText
android:id="@+id/trt_edt"
android:hint="IP address or hostname"
android:textSize="@dimen/a_normal"
android:layout_width="260dp"
android:layout_toRightOf="@id/trt_start"
android:layout_alignBottom="@id/trt_start"
android:layout_marginLeft="15dp"
android:inputType="textAutoComplete"
android:layout_height="wrap_content"/>