1.
dialEtBox.addTextChangedListener(new PhoneNumberWatcher());
2.
class PhoneNumberWatcher implements TextWatcher {
// 为什么会执行两次
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
System.out.println("beforeTextChanged--"
+ dialEtBox.getText().toString());
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
System.out.println("onTextChanged--"
+ dialEtBox.getText().toString());
}
public void afterTextChanged(Editable s) {
System.out.println("afterTextChanged--"
+ dialEtBox.getText().toString());
}
}
问题不是出自上面,而是,文本框中的文字改变的时候
public void updateEditText(String inputNumber)
{
String newNumber = dialEtBox.getText().append(inputNumber).toString();//问题所在!
dialEtBox.setText(newNumber);
}
改变EditText调用的是上面的方法
当执行
dialEtBox.getText().append(inputNumber)
实际上这里就已经执行了一次TextWatcher!!!
dialEtBox.setText(newNumber);
又执行了一次
需要注意的是:
dialEtBox.getText();返回的不是String,而是一个Editable对象
要将这个对象toString()之后再append内容!
改为这样就好了:
String newNumber =new StringBuilder(dialEtBox.getText().toString()).append(inputNumber).toString();
注意:先将EditText中的内容转为字符串dialEtBox.getText().toString()
,而不是仅仅dialEtBox.getText();
,这样返回的是一个Editable对象,对这个对象的append会内TextWatcher接收到!
分享到:
相关推荐
editText1.addTextChangedListener(textWatcher); editText2.addTextChangedListener(textWatcher); editText3.addTextChangedListener(textWatcher); // TextWatcher实现 TextWatcher textWatcher = new ...
前言 最近在开发一个IM项目的时候有一个需求就是,好友搜索功能。即在EditText中输入好友名字,ListView列表中动态...searchEdittext.addTextChangedListener(new TextWatcher() { @Override public void onTextCha
3. 使用addTextChangedListener方法将监听器实例添加到EditText控件上。 4. 自定义TextWatcher类,并实现TextWatcher接口的三个方法。在onTextChanged方法中添加代码逻辑以实现字数限制功能。 在自定义的...
- **匿名内部类**:直接在`EditText`对象上调用`addTextChangedListener`方法,创建一个匿名内部类实现`TextWatcher`接口。 ```java editText.addTextChangedListener(new TextWatcher() { @Override public ...
此外,我们还可以监听EditText的`addTextChangedListener`事件,以便在文本发生变化时执行相应的操作。 为了实现自动清空EditText的功能,我们通常会利用TextWatcher接口。TextWatcher提供了三个方法:`...
phoneNumberEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged...
editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // 在文本改变前被调用 } @Override public void ...
我们可以为每个`EditText`添加一个`TextWatcher`,并在`afterTextChanged()`方法中检查所有`EditText`是否都有内容: ```java EditText edt1 = findViewById(R.id.edt1); EditText edt2 = findViewById(R.id.edt2);...
editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s...
editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // 在文本改变前执行的操作 } @Override public void...
2. **标记法**:设置一个标志变量,当一个`EditText`发生变化时,设置标志,其他`EditText`的监听器检测到标志后不执行计算,直到标志被清除。 例如,使用延时计算的实现: ```java Handler handler = new Handler...
editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s,...
editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s,...
editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s,...
editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s...
editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s,...
editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s...
editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s...
editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s,...
editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s,...