【问题标题】:Replacing character in Edittext while typing键入时替换 Edittext 中的字符
【发布时间】:2020-04-30 11:12:24
【问题描述】:

我有一个函数,当用户选择选项 1 时返回字母“A”的一些字符串,当用户选择选项 2 时返回一些不同的字符串:

private String changeText(int option){
if(option==1)
    return "Y";
if(option==2)
    return "Z";
}

当用户选择选项 1 并键入“A”时,我想替换 EdittextView 中的字符,将 A 替换为“Y”,将选项 2 替换为“Z”,这是实时完成的。 所以我想出了TextWatcher。

@Override
public void afterTextChanged(Editable s) {

    if (s.length() == 0)
      return;

     s.replace(editText.getSelectionStart(),    
     editText.getSelectionStart()+1, changeText(option));                                                                                                                                                                   

}

它不工作。我的猜测是我试图在打印之前用较新的字符替换字符(不确定)。我只想根据选择的选项替换任何光标位置最后输入的字符。

【问题讨论】:

  • s.replace(editText.getSelectionStart(), editText.getSelectionStart()+1, changeText(option));行中的option是什么以及如何获取值
  • 是 1 或 2

标签: android android-edittext textwatcher


【解决方案1】:

你试过了吗

beforeTextChanged(CharSequence s, int start, int count, int after)

在此方法中,您可以在渲染之前更改符号。获取 CharSequence 并在位置更改字符(开始,开始+计数)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-19
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    相关资源
    最近更新 更多