【问题标题】:afterTextChange in TextWatcher how handle backspace and setText()?TextWatcher中的afterTextChange如何处理退格和setText()?
【发布时间】:2016-10-04 14:03:31
【问题描述】:

我有一个 EditText,我使用 afterTextChange() 逐个字符地获取每个字符的值。好吧,我有一个按钮要做editText.setText(""),但是当我使用它时,我得到了这个错误

charAt: 0 >= length 0.

如何处理此错误以及如何处理退格?

我的代码是:

 @Override
            public void afterTextChanged(Editable s) {


                enigmaCoding(String.valueOf(s.charAt(ii)));
                ii++;

            }

        });


  public void restart(View view) {

        ii = 0;
        txtV.setText("");
        etxtC.setText("");

【问题讨论】:

    标签: android user-interface text android-edittext


    【解决方案1】:

    你可以检查

    if(s!=null && s.toString().length()>0)
    {
      currentLength = s.toString().length();
      if(currentLength < previousLength){
          --ii;
      }else{
         enigmaCoding(String.valueOf(s.charAt(ii)));
         ii++;
     }
        previousLength = currentLength;// store previous length
    }
    

    【讨论】:

    • tnx man 它适用于 setText(""),我如何处理 backSpace,当我使用 backSpace 时出现此错误 charAt: 7 >= length 6 @N J
    • still charAt: 5 >= length 4 @N J 并且当我使用空间时,它会复制我在另一个 textView 中使用的最后一个字符
    • 再次编辑答案
    • 删除时有效,再次输入时出现此错误 charAt: 1 >= length 1@ N J
    猜你喜欢
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 2012-08-25
    • 2012-03-16
    • 2012-10-04
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    相关资源
    最近更新 更多