gpt4 book ai didi

java - 在 multiautocompletetextview 中输入双空格来替换逗号

转载 作者:行者123 更新时间:2023-12-02 03:14:26 24 4
gpt4 key购买 nike

当我在 multiautocompletetextview 中从键盘输入双空格时,我想添加逗号。我在谷歌上搜索了很多东西。但达不到我的目标。我想替换用户输入的双空格的逗号。

很明显,我必须在 addtextwatcher 监听器的 ontextChange() 或 OnAfterTextChanged() 中编写一些逻辑。但是我没有收到添加 2 个空格后的事件。

当从列表中选择单词时,我已经使用了逗号标记器。但是我想在用户使用键盘输入双空格时添加逗号。

提前致谢

最佳答案

我可以为您提供的最简单的解决方案是使用String.replace(),这是可以帮助您的小代码 fragment

 @Override
protected void onCreate(Bundle savedInstanceState) {

...

edt.addTextChangedListener(textWatcher);
}

以及您要在 EditText 上设置的 TextWatcher

TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
edt.removeTextChangedListener(textWatcher);
String text = edt.getText().toString();
text = text.replace(" ", ",");
edt.setText(text);
edt.setSelection(text.length());
edt.addTextChangedListener(textWatcher);
}

@Override
public void afterTextChanged(Editable editable) {

}
};

enter image description here

关于java - 在 multiautocompletetextview 中输入双空格来替换逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40520817/

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