我正在使用类似于 SO 的标记系统,而不是强制用户在他们的键盘中找到破折号,我希望空格自动转换为破折号。
我正在尝试使用此 textWatcher 实现它,但该应用程序不允许我键入空格键(它有点闪烁但没有任何反应。
imageTagsInput.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
imageTagsInput.removeTextChangedListener(this)
imageTagsInput.setText(imageTagsInput.text.toString().replace(" ","-"))
imageTagsInput.addTextChangedListener(this)
imageTagsInput.setSelection(imageTagsInput.length())
}
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
})
这是 EditText 的 xml:
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789- "
最佳答案
在 afterTextChanged 中添加这样的内容
String result = s.toString().replaceAll(" ", "-");
if (!s.toString().equals(result)) {
ed.setText(result);
ed.setSelection(result.length());
// alert the user
}
有关更多信息,请参阅此 answer
关于android - 如何在用户键入时用不同的注释替换一个注释(将 “space” 替换为 “-”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55945720/