android – TextWatcher onTextChanged无法使用软键盘自动完成/建议的单词
作者:互联网
我正在EditText上实现TextWatcher,以便在用户输入新字符时查找文本中的一系列关键字并为其加下划线.但是,当选择软键盘上的建议/自动完成字时,不是将建议的字添加到Edittext然后调用onTextChanged函数,而是删除半完整字.我觉得这很奇怪,因为输入单个字符可以很好地激活onTextChanged函数.任何帮助将不胜感激.
PS.如果有人知道在飞行中处理EditText的更好方法,请告诉我.
码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_codify_test);
final EditText editText = (EditText) findViewById(R.id.editText_codifyTest);
editText.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 (!checked) { //stop infinite loop
checked = true;
cursorPosition = editText.getSelectionStart(); //get cursor position before text modification
codifyText(editText);
} else {
checked = false;
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
//Find and underline keywords
private void codifyText(EditText editText) {
String plainText = editText.getText().toString() + " ";
int prevWhiteSpace = 0;
final Context context = this;
SpannableString codifiedText = new SpannableString(plainText.substring(0, plainText.length() - 1));
if (codifiedText.length() == 0) return;
for (int i = 0; i < plainText.length(); i ++){
if (Character.isWhitespace(plainText.charAt(i))){
String currWord = plainText.substring(prevWhiteSpace, i);
if (isKeyWordInDataBase(currWord)) {
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View view) {
}
};
codifiedText.setSpan(clickableSpan, prevWhiteSpace, i, 0);
}
prevWhiteSpace = i + 1;
}
}
editText.setMovementMethod(LinkMovementMethod.getInstance());
editText.setText(codifiedText, TextView.BufferType.SPANNABLE);
editText.setSelection(cursorPosition); //set cursor to position prior to edit
}
解决方法:
最好去afterTextChanged方法.
在下面的示例中,将使用Handler检索和处理输入的文本以进行进一步处理
EditText text1;
StringBuffer previousChar=new StringBuffer();
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
text1=(EditText)findViewById(R.id.editText);
text1.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) {
}
@Override
public void afterTextChanged(Editable s) {
if(!previousChar.toString().equals(s.toString())){
Message msg=new Message();
msg.obj=s.toString();
localHandler.sendMessage(msg);
previousChar=new StringBuffer(s.toString());
Log.i("TAG", "TextEntered = "+s);
}
}
});}
并在处理程序中
private Handler localHandler = new Handler(){
public void handleMessage(Message msg) {
super.handleMessage(msg);
String value=(String)msg.obj;
//Your logic with the text entered that is retrieved
}
};
标签:android,android-softkeyboard,android-edittext,textwatcher,android-textwatcher 来源: https://codeday.me/bug/20190702/1356330.html