在设置EditText中设置了这些ime的属性,在点击键盘的回车键时候,会导致 focus search returned a view that wasn't able to take focus!异常的出现
查看TextVie源码发现在点击键盘的回车键会通过EditableInputConnection
来执行onEditorAction
方法
public void onEditorAction(int actionCode) {//设置了ime属性mEditor就不为空final Editor.InputContentType ict = mEditor == null ? null : mEditor.mInputContentType;if (ict != null) {if (EditorActionListener != null) {//利用这里的return解决异常if (EditorAction(this,actionCode, null)) {return;}}if (actionCode == EditorInfo.IME_ACTION_NEXT) {View v = focusSearch(FOCUS_FORWARD);if (v != null) {//异常原因if (!v.requestFocus(FOCUS_FORWARD)) {throw new IllegalStateException("focus search returned a view "+ "that wasn't able to take focus!");}}return;} else if (actionCode == EditorInfo.IME_ACTION_PREVIOUS) {View v = focusSearch(FOCUS_BACKWARD);if (v != null) {//异常原因if (!v.requestFocus(FOCUS_BACKWARD)) {throw new IllegalStateException("focus search returned a view "+ "that wasn't able to take focus!");}}return;} else if (actionCode == EditorInfo.IME_ACTION_DONE) {InputMethodManager imm = InputMethodManager.peekInstance();if (imm != null && imm.isActive(this)) {imm.hideSoftInputFromWindow(getWindowToken(), 0);}return;}}//省略不重要的代码}
复制代码
从代码可以看出actionCode == EditorInfo.IME_ACTION_PREVIOUS
和actionCode == EditorInfo.IME_ACTION_PREVIOUS
这里可能会出现异常,异常的原因是查询上一个或下一个view的焦点的时候失败了 抛出了异常
这两处抛出异常的代码上方有个return,如果能保证onEditorActionListener()
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {@Overridepublic boolean onEditorAction(TextView v, int actionId, KeyEvent event) {//可以根据需求获取下一个焦点还是上一个View nextView = v.focusSearch(View.FOCUS_DOWN);if (nextView != null) {questFocus(View.FOCUS_DOWN);}//这里一定要返回truereturn true;}});
复制代码
本文发布于:2024-01-29 09:29:34,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170649177714323.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |