【问题标题】:Text watcher in AndroidAndroid中的文本观察器
【发布时间】:2013-12-06 04:15:52
【问题描述】:

我有一个程序,其中有一个 editText 。我想实现 TextWatcher 以反映总编辑文本的变化。我想在编辑框达到最大字符限制 10 时显示警报对话框。当我实现文本观察器时它可以工作,但是当它达到 10 个字符时它会显示两个警报框。这是我的代码

private TextWatcher mTextEditorWatcher = new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // This sets a textview to the current length
        // mTextView.setText(String.valueOf(s.length()));

    }

    public void afterTextChanged(Editable s) {
        if (s.length() == 10) {
            messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
            AlertDialog.Builder alert = new AlertDialog.Builder(
                    SendSms.this);

            alert.setMessage("You Cross the limit of 10 Words !");
            alert.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {

                        }
                    });

            alert.show();

        }

    }
};

【问题讨论】:

  • android:inputType="textNoSuggestions" 添加到 EditText。这将起作用。无意义的解决方案,但有效
  • 你应该使用 Toast 或 mTextEditorWatcher.showError("") 而不是 AlertDialog;
  • 在 EditText 的 XML 属性中,您可以添加:android:maxLength="10"
  • 我和@SilentKiller 在一起,他是对的……更好的对话
  • @Gauravkumar 你想显示警报..??

标签: android android-edittext textwatcher


【解决方案1】:

这是此方法的常见行为。你需要使用flag来处理。

使用此代码

   private TextWatcher mTextEditorWatcher = new TextWatcher() {
        boolean flag = false; 

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

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

    @Override
    public void afterTextChanged(Editable s) {
        if (s.length() == 10) {
            if (!flag) {
                messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
                AlertDialog.Builder alert = new AlertDialog.Builder(
                        SendSms.this);

                alert.setMessage("You Cross the limit of 10 Words !");
                alert.setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {

                            }
                        });

                alert.show();
            }
            flag = true; 
        } else {
            flag = false; 
        }

    }
});

其他参考

TextWatcher events are being fired multiple times

Events of TextWatcher are being called twice


最好使用setError。阅读how to use it.

【讨论】:

  • 不不不。用这个替换你的代码。查看我更新代码时的内容
  • 哇!那很好!。 Congs.. 还有一件事你可以做的是s.length() >= 10 而不是s.length() == 10
  • 先生,我想使用编辑文本下方的测试视图。这表明还剩下 150,149 个字符。你能帮我吗??
  • 是的先生 :).. 阅读此stackoverflow.com/questions/3013791/…。看到他们添加了mTextView.setText(String.valueOf(s.length()));。这里mTextView我是要显示字符数的TextView。
  • 阅读以上内容,如果有任何问题告诉我,会尝试清除 :)
【解决方案2】:
 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
             SendSms.this);
     AlertDialog alertDialog ;

 private TextWatcher mTextEditorWatcher = new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // This sets a textview to the current length
            // mTextView.setText(String.valueOf(s.length()));

      if (s.length() >= 10) {
                messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
                        alertDialogBuilder.setMessage("You Cross the limit of 10 Words !");
         alertDialogBuilder.setPositiveButton("OK",
                 new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog,
                             int whichButton) {

                     }
                 });

         alertDialog = alertDialogBuilder.create();
         if(alertDialog != null  && !alertDialog.isShowing()){
             alertDialog.show();
         }



            }
    else
    {
       mTextView.setText(String.valueOf(s.length()));
    }



        }

        public void afterTextChanged(Editable s) {


        }
    };

我已经编辑了代码。全局声明alertDialog,检查alertDialog是否已经显示,然后显示alert

【讨论】:

  • 没有 afterTextChange() 就无法实现文本观察器。
  • @Gauravkumar 他仍然有方法。只需添加它,其中不包含任何代码。
【解决方案3】:

试试这个..

你试过onTextChanged

private TextWatcher mTextEditorWatcher = new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // This sets a textview to the current length
        // mTextView.setText(String.valueOf(s.length()));


        if (s.length() == 10) {
            messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
            AlertDialog.Builder alert = new AlertDialog.Builder(
                    SendSms.this);

            alert.setMessage("You Cross the limit of 10 Words !");
            alert.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {

                        }
                    });

            alert.show();

        }

    }

    public void afterTextChanged(Editable s) {

    }
};

【讨论】:

  • 为什么这个工作而不是afterTextChanged?它们都应该给出相同的结果。
  • @Gauravkumar 如果您输入 10 个字母,它将显示一个对话框,然后如果您按 ok,然后您输入额外的 1 个字母,然后您擦除该字母意味着然后对话框将显示。
【解决方案4】:

只需将警报实例设为全局即可正常工作。

    private AlertDialog.Builder alert ;

在你的 onCreate 中

    alert = new AlertDialog.Builder(
                 RegisterActivity.this);

在你的 afterTextChanged 中

    public void afterTextChanged(Editable s) {
        if (s.length() == 10) {
            //messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
            alert.setMessage("You Cross the limit of 10 Words !");
            alert.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {

                        }
                    });

            alert.show();

        }

    }

【讨论】:

  • Gaurav 在我的情况下工作正常。不要放 AlertDialog.Builder(RegisterActivity.this);在你的 afterTextChanged() 方法中
【解决方案5】:

嗨,我为电话号码做了这样的工作,当它达到 5 位时,它会自动放置空格。您可以使用我的代码。请检查以下代码。

public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            if (s.length() == 5 && start == 4) {
                txtNumber.removeTextChangedListener(this);
                txtNumber.append(" ");
                txtNumber.addTextChangedListener(this);
            }
            if (s.length() == 6 && start == 5) {
                txtNumber.removeTextChangedListener(this);
                String newtext = s.toString().substring(0, s.length() - 1)
                        + " " + s.charAt(s.length() - 1);
                txtNumber.setText("");
                txtNumber.append(newtext);
                txtNumber.addTextChangedListener(this);
            }
        }

如果您需要更多帮助,请告诉我

【讨论】:

    【解决方案6】:

    尝试以下代码:

    全局实例:

    private AlertDialog.Builder alert;
    

    TextWatcher:

    private TextWatcher mTextEditorWatcher = new TextWatcher() {
    
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
    
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() >= 10) {
                messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
                // alert = new AlertDialog.Builder(MainActivity.this);
                // alert.setMessage("You Cross the limit of 10 Words !");
                // alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                // public void onClick(DialogInterface dialog, int whichButton) {
                // }
                // });
                // alert.show();
    
                // Else you should use the following one to show error message
                messageBody.setError("You Cross the limit of 10 Words !");
    
            } else {
                messageBody.setError(null);
            }
    
        }
    
        public void afterTextChanged(Editable s) {
        }
    };
    

    【讨论】:

      猜你喜欢
      • 2018-07-11
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      • 2016-11-05
      • 2019-01-14
      相关资源
      最近更新 更多