【问题标题】:replace character inside TextWatcher in android在android中替换TextWatcher中的字符
【发布时间】:2013-03-17 05:28:14
【问题描述】:

我使用 TextWatcher 来更改按键值。我的目标是在打字时替换一些字符。例如,当我键入键时,如果达到“S”字符,则将其替换为“a”字符。我的问题是:我应该在 beforeTextChanged 中做吗?如何?谁能给我一个例子?

【问题讨论】:

  • 尝试使用afterTextChanged而不是beforeTextChanged来替换EditText中的字符
  • 我试图这样做,但我无法得到我想要的。可以举个例子吗?
  • 请解释一下如果你使用 afterTextChanged 会得到什么以及你想得到什么帮助

标签: java android textwatcher


【解决方案1】:

我知道这篇文章已经有几年的历史了,但两个版本都不适合我,并且在两个答案之间建立了一个混合体。

@Override
public void afterTextChanged(Editable editable) {    
    if (editable.toString().contains(",")) {
       Editable ab = new SpannableStringBuilder(editable.toString().replace(",", ""));
       editable.replace(0, editable.length(), ab);
    }
}

【讨论】:

  • 这比接受的答案要好,因为它不会将光标位置移到前面。
  • 就我而言(不检查,),我还需要在此方法的第一行中执行editTextView.removeTextChangedListener(this);,以防止一遍又一遍地无限调用此方法。
  • 建议在自定义TextWatcher中创建布尔值以防止无限循环,Android团队的示例类:PhoneNumberFormattingTextWatcher
【解决方案2】:

使用 beforeTextChanged 不会有用,因为它不会中断 EditText 键的实际打印。我会使用类似的东西:

    public void afterTextChanged(Editable s) {
        if(s.length() > 0 && s.toString().charAt(s.length()-1) == 'S')
        {
            final String newText = s.toString().substring(0, s.length()-1) + "a"; 
            editText.setText(newText); 
        }
    }

我添加了一些 toString(),不是 100% 确定 Editable 是如何工作的,但我认为应该涵盖它。

【讨论】:

  • “量子应用程序”下面的答案是优越的。 TextWatcher 不需要持有对 editText 实例的引用,从 TextWatcher 调用 editText.setText 会产生光标问题。
【解决方案3】:

您必须在afterTextChanged 中进行操作,但不要忘记分离 并重新附加 TextChangedListener 以防止无限循环。
一个简单的示例如下所示:

public void afterTextChanged(Editable s) {
        editText.removeTextChangedListener(this);
        //.. do changes here ..//
        editText.setText(newText);
        editText.addTextChangedListener(this);
}

但是当你调用setText 时还有另一个问题,它会导致光标移动到textView 内文本的末尾。所以你必须自己计算光标的新位置,记住用户可以通过粘贴或删除文本的选定部分来一次输入多个字符。
这是一个更完整的例子。此观察程序类删除文本的所有非数字字符。

public class NumWatcher implements TextWatcher {
    private EditText editText;
    private int selPos;
    private String oldString, newString;
    public NumWatcher(EditText editText) {
        this.editText = editText;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        selPos = editText.getSelectionStart();
        oldString = myFilter(s.toString());
    }

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

    @Override
    public void afterTextChanged(Editable s) {
        editText.removeTextChangedListener(this);
        newString = myFilter(s.toString());
        editText.setText(newString);
        int newPos = selPos + (newString.length() - oldString.length());
        if (newPos < 0) newPos = 0;
        editText.setSelection(newPos);
        editText.addTextChangedListener(this);
    }

    public String myFilter(String s) {
        String digits;
        digits = s.replaceAll("[^0-9]", "");
        if (s.equals("")) return "";
        return digits;
    }
}

【讨论】:

  • 建议不要在 TextWatcher 中保存 EditText 的引用,而是创建布尔值以防止无限循环。看看 Android 团队的 PhoneNumberFormattingTextWatcher
【解决方案4】:
@Override
public void afterTextChanged(Editable arg0) {
    Editable ab = new SpannableStringBuilder(arg0.toString().replace("S", "a"));
    arg0 = ab ;
}

【讨论】:

  • 重新分配局部变量不会影响原来的 Editable 值!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2012-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
  • 1970-01-01
相关资源
最近更新 更多