【发布时间】: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