Java TextWatcher-替换字符串的最后一个字符

Java TextWatcher-替换字符串的最后一个字符,java,textwatcher,Java,Textwatcher,每次“q”作为我附加TextWatcher的edittext的最后一个字符写入时,“q”将被替换为“a”。我使用: 但是,当我测试代码时,当我输入“q”时,什么都没有发生。要帮忙吗?非常感谢不要使用后文本更改,它是危险的,因为它将被递归调用,您可能会陷入无限循环 正如文件所说: It is legitimate to make further changes to s from this callback, but be careful not to get yourself

每次“q”作为我附加TextWatcher的edittext的最后一个字符写入时,“q”将被替换为“a”。我使用:


但是,当我测试代码时,当我输入“q”时,什么都没有发生。要帮忙吗?非常感谢

不要使用
后文本更改
,它是危险的,因为它将被递归调用,您可能会陷入无限循环

正如文件所说:

     It is legitimate to make further changes to s from this callback,
    but be careful not to get yourself into an infinite loop, because any 
   changes you make will cause this method to be called again recursively
改用
onTextChanged

在你的问题上

您忘记在字符串中添加字符
a

public void onTextChanged (CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub
    if(s.length() > 0 && s.toString().charAt(s.length()-1) == 'q')
    {
        current_string = s.toString();
        current_string = current_string.substring(0, (current_string.length()-1));
        et.setText(current_string + "a"); //add the a after q is deleted
        }
    }
}

您忘记在
substring()
之后追加
a
,您应该使用
StringBuilder
,快捷方式是
et.setText(当前字符串+“a”)
public void onTextChanged (CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub
    if(s.length() > 0 && s.toString().charAt(s.length()-1) == 'q')
    {
        current_string = s.toString();
        current_string = current_string.substring(0, (current_string.length()-1));
        et.setText(current_string + "a"); //add the a after q is deleted
        }
    }
}