gpt4 book ai didi

android - EditText - 光标在明文时开始显示每个字母

转载 作者:行者123 更新时间:2023-11-30 00:45:03 28 4
gpt4 key购买 nike

我将 TextWatcher 设置为 EditText,如下所示。但是当我尝试清除文本时,光标会在清除每个字母后开始。

  class MyInputWatcher implements TextWatcher {

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {

}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
et.removeTextChangedListener(watcher2);
et.setText(s.toString().replaceAll("[^[:alpha:]]", ""));
et.addTextChangedListener(watcher2);
}
@Override
public void afterTextChanged(Editable s) {

}
}

最佳答案

这样做(更新):

class MyInputWatcher implements TextWatcher {

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String temp = s.toString().replaceAll("[^a-zA-Z]", "");
if (s.toString().length() != temp.length()) {
et.setText(temp);
et.setSelection(temp.length());
}
}

@Override
public void afterTextChanged(Editable s) {
}
}

关于android - EditText - 光标在明文时开始显示每个字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41953259/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com