gpt4 book ai didi

android - Android 中的 TextWatcher 警告和慢速类型

转载 作者:行者123 更新时间:2023-11-29 19:17:24 25 4
gpt4 key购买 nike

我想在 android 中更改字符类型。如果用户键入 space- 它将更改为 _

所以我尝试了:

TextWatcher tt = null;

final EditText etUsername = (EditText) findViewById(R.id.etUsername);
tt = new TextWatcher() {
public void afterTextChanged(Editable s){
etUsername.setSelection(s.length());
}
public void beforeTextChanged(CharSequence s,int start,int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count) {
etUsername.removeTextChangedListener(tt);
etUsername.setText(etUsername.getText().toString().replace(" ", "_"));
etUsername.setText(etUsername.getText().toString().replace("-", "_"));
etUsername.addTextChangedListener(tt);
}
};
etUsername.addTextChangedListener(tt);

它“有效”,但如果用户输入速度很快,一些字母将不会出现,我会收到一些警告:

W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: commitText on inactive InputConnection
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: commitText on inactive InputConnection
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: commitText on inactive InputConnection
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: commitText on inactive InputConnection
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
W/IInputConnectionWrapper: finishComposingText on inactive InputConnection

有什么问题吗?

最佳答案

无需在文本更改时一次又一次地删除和添加文本更改监听器,只需设置一个条件来检查您的可编辑项是否具有“-”或“”,然后只需将其替换并设置为 EditText。

etUsername.addTextChangedListener(new 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) {
if (s.length() > 0 && (s.toString().contains("-") || s.toString().contains(" "))) {
etUsername.setText(s.toString().replace("-", "_").replace(" ", "_"));
}
}

@Override
public void afterTextChanged(Editable s) {
etUsername.setSelection(s.length());
}
});

关于android - Android 中的 TextWatcher 警告和慢速类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43088263/

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