我正在EditText上实现TextWatcher,以便在用户输入新字符时查找文本中的一系列关键字并为其加下划线.但是,当选择软键盘上的建议/自动完成字时,不是将建议的字添加到Edittext然后调用onTextChanged函数,而是删除半完整字.我觉得这很奇怪,因为输入单个字符可以很好地激活onTextChanged函数.任何帮助将不胜感激.
PS.如果有人知道在飞行中处理EditText的更好方法,请告诉我.
码:
@OverrIDeprotected 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 keywordsprivate 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 – TextWatcher onTextChanged无法使用软键盘自动完成/建议的单词全部内容,希望文章能够帮你解决android – TextWatcher onTextChanged无法使用软键盘自动完成/建议的单词所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)