代码之家  ›  专栏  ›  技术社区  ›  AdamJones

edittext的setText导致addTextChangedListener崩溃

  •  2
  • AdamJones  · 技术社区  · 14 年前

    我试图剥夺所有非标准的字母字符从一个android应用程序的编辑文本框我做。我成功地创建了一个监听器,通过正则表达式获得了值并删除了错误的chr。但是,下面的.setText行会导致应用程序崩溃。有人知道如何绕过这一点,动态屏蔽某些chr吗?

    filenameTextBox.addTextChangedListener(new TextWatcher() { 
    
        public void onTextChanged(CharSequence s, int start, int before, int count) {
    
            FILENAME=s.toString();
            FILENAME = FILENAME.replaceAll("[^a-zA-Z]", "");
            filenameTextBox.setText(FILENAME);
       }
    }
    
    3 回复  |  直到 6 年前
        1
  •  10
  •   Pawan Maheshwari    13 年前

    如果我能清楚地理解你的问题,希望这段话能对你有所帮助

            public void afterTextChanged(Editable editable)
            {
                if (editable.length() != 0)
                {
                    chatTextArea.removeTextChangedListener(this);
                    chatTextArea.setText("your text");
                    chatTextArea.addTextChangedListener(this);
                }
            }
    
        2
  •  0
  •   GôTô    14 年前

        3
  •  0
  •   Vladimir Mamulov    11 年前
    public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            chatTextArea.removeTextChangedListener(this);
            String s_new = s.toString().replaceAll("[^0-9]", ""); // for example, if need
            chatTextArea.setText(s_new);
            chatTextArea.setSelection(start + count + s_new.length() - s.length());
            chatTextArea.addTextChangedListener(this);
        }