【问题标题】:using setText() don't want to call textwatcher events? [duplicate]使用 setText() 不想调用 textwatcher 事件? [复制]
【发布时间】:2015-11-20 05:15:25
【问题描述】:

我正在使用 EditText。当我使用 setText() 时。 TextWatcher 事件正在调用。 我不需要调用它?谁能帮帮我?

        txt_qty.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

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

谢谢。

【问题讨论】:

  • 什么时候调用?

标签: android android-edittext settext android-textwatcher


【解决方案1】:

您可以取消注册观察者,然后重新注册。

要注销观察者,请使用以下代码:

txt_qty.removeTextChangedListener(yourTextWatcher);

使用以下代码重新注册:

txt_qty.addTextChangedListener(yourTextWatcher);

或者,您可以设置一个标志,以便您的观察者知道您何时自己更改了文本(因此应该忽略它)。

在您的活动中定义一个标志是: boolean isSetInitialText = false;

当你在调用 set text 之前调用 txt_qty.settext(yourText) 时,请在调用 isSetInitialText = true 时,

然后将您的观察者更新为:

txt_qty.addTextChangedListener(new TextWatcher() {
            @Override 
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
          if (isSetInitialText){
                isSetInitialText = false;
          } else{
                  // perform your operation
          }

            @Override 
            public void onTextChanged(CharSequence s, int start, int before, int count) {
              if (isSetInitialText){
                   isSetInitialText = false;
               } else{
                 // perform your operation
               }
            } 

            @Override 
            public void afterTextChanged(Editable s) {
                 if (isSetInitialText){
                      isSetInitialText = false;
                 } else{
                     // perform your operation
                 }
            } 
        }); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多