focus search returned a view that wasn't able to take focus!

阅读: 评论:0

focus search returned a view that wasn't able to take focus!

focus search returned a view that wasn't able to take focus!

Android在 ListView或RecyclerView 的item中EditText 设置imeOptions、imeActionId、imeActionLabel属性导致的异常分析与处理

在设置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,如果能保证
永远为true那么异常代码就永远不会执行 即设置EditText的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小时内删除。

标签:search   focus   view   returned
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23