Java 在EditText中输入返回键时,TextWatcher会运行多次
我和TextWatcher有一个EditTextJava 在EditText中输入返回键时,TextWatcher会运行多次,java,android,android-edittext,textwatcher,Java,Android,Android Edittext,Textwatcher,我和TextWatcher有一个EditText 场景1: 编辑包含“abcd的文本 如果我按回车键或输入换行符 1) 在字符之前,TextWatcher会触发3次 2) 在字符之间,TextWatcher激发4次 3) 在字符末尾,TextWatcher触发1次 场景2: 编辑包含“1234”的文本 如果我按回车键或输入换行符 1) 在字符之前,TextWatcher触发1次 2) 在字符之间,TextWatcher触发1次 3) 在字符末尾,TextWatcher触发1次 这是虫子吗
场景1: 编辑包含“abcd的文本 如果我按回车键或输入换行符 1) 在字符之前,
TextWatcher
会触发3次
2) 在字符之间,TextWatcher
激发4次
3) 在字符末尾,TextWatcher
触发1次
场景2: 编辑包含“1234”的文本 如果我按回车键或输入换行符 1) 在字符之前,
TextWatcher
触发1次
2) 在字符之间,TextWatcher
触发1次
3) 在字符末尾,TextWatcher
触发1次
这是虫子吗 还是有什么我不明白的
我希望文本监视程序在所有场景中只触发一次
任何帮助都将不胜感激。这是因为数字被计算为单个值,即数字1或12“12”,而不是1,2。相反,当您输入单词“Strings”时,它们被划分为多个字符,整个字符串中的字符总数将在textWatcher重载方法的count参数中返回 e、 g如果输入123,它将被解释为单个值一百二十三。因此,计数返回为1。当你输入hello时,它被分为单独的字符,即“h”,“e”,“l”,“l”,“o”,总共5个字符。因此,总计数返回为5
希望这个解释能有所帮助。我找到了解决方案,但可能并不适合所有需求 之前,当
TextWatcher
被多次触发,其中的代码也被多次执行时,我与
editText.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
Log.e(TAG, "111 text =---------------" + charSequence);
}
public void onTextChanged(CharSequence charSequence, int start, int before, int count){
Log.e(TAG, "222 text =---------------" + charSequence);
}
public void afterTextChanged(Editable editable) {
Log.e(TAG, "333 text ---------------" + editable);
}
});
现在,根据我的要求,我找到了解决方案,我同意了
editText.addTextChangedListener(new TextWatcher() {
String initialText = "";
private boolean ignore = true;
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
if ( initialText.length() < charSequence.length() ){
initialText = charSequence.toString();
Log.e(TAG, "111 text ---------------" + charSequence);
}
}
public void onTextChanged(CharSequence charSequence, int start, int before, int count){
if( initialText.length() < charSequence.length() ) {
initialText="";
ignore=false;
Log.e(TAG, "222 text ---------------" + charSequence);
}
}
public void afterTextChanged(Editable editable) {
if(!ignore) {
ignore = true;
Log.e(TAG, "333 text ---------------" + editable);
}
}
});
editText.addTextChangedListener(新的TextWatcher(){
字符串initialText=“”;
私有布尔忽略=真;
更改前文本之前的公共void(CharSequence CharSequence、int start、int count、int after){
if(initialText.length()
现在,
TextWatcher
也在触发多次,但是对于我在问题中提到的所有场景,if条件中的代码只执行一次 你能发布你的文本更改监听器和文本观察者代码吗?在TextWatcher
中,定位是有用的,然后进行评估。文本观察程序返回1234和abcd的相同位置。那么它也会考虑赋值吗?在场景1中,你可以看到所有的字母都是数字的。那么,为什么在启动这项活动方面存在如此大的差异呢?