如何避免在Android的EditText中执行onTextChanged [英] How can I avoid execution of onTextChanged in Android's EditText

查看:670
本文介绍了如何避免在Android的EditText中执行onTextChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何避免这样的代码行:

How can I avoid that a code line like:

((EditText) findViewById(R.id.MyEditText)).setText("Hello");

将在此处引发事件:

((EditText) findViewById(R.id.MyEditText)).addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start,
    int before, int count) {
// HERE
}

@Override
public void beforeTextChanged(CharSequence s, int start,
    int count, int after) {
}

@Override
public void afterTextChanged(Editable s) {
}
});

我想知道是否有任何方法可以抑制onTextChanged的执行,正如我在选择AutoCompleteTextView的下拉结果(不执行onTextChanged!)的情况下注意到的那样.

I want to know if there is any way to inhibit the execution of onTextChanged as I noticed in the case of selecting a AutoCompleteTextView's dropdown result (no onTextChanged is executed!).

我不是在寻求如果你好,什么都不做"之类的解决方法...

I'm not seeking for workarounds like "if hello do nothing"...

推荐答案

AutoCompleteTextView 显示他们设置了一个布尔值,以表示该文本已被块完成替换

The Source for AutoCompleteTextView shows that they set a boolean to say the text is being replaced by the block completion

         mBlockCompletion = true;
         replaceText(convertSelectionToString(selectedItem));
         mBlockCompletion = false;    

这是实现任何您想要做的事的好方法. TextWatcher 然后检查以查看setText是否通过补全到达并返回方法之外

This is as good a way as any to achieve what you want to do. The TextWatcher then checks to to see if the setText has come via a completion and returns out of the method

 void doBeforeTextChanged() {
     if (mBlockCompletion) return;

添加和删除TextWatcher对于该应用程序将更加耗时

Adding and removing the TextWatcher will be more time consuming for the application

这篇关于如何避免在Android的EditText中执行onTextChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆