在 Android P 中:由于没有窗口焦点而丢弃事件:KeyEvent [英] In Android P: Dropping event due to no window focus: KeyEvent
问题描述
在 Android P
中,应用程序在启动时无法按预期工作并抛出警告:
In Android P
, application on start not working as expected with throwing warnings :
W/ViewRootImpl:
Dropping event due to no window focus: MotionEvent { action=ACTION_DOWN, KeyCode = DPAD_DOWN}
和
W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_UP, KeyCode = DPAD_DOWN} and its not allowing any action to performed.
检查图像以获取详细警告.
Check Image for detailed warning.
注意:相同的应用程序在 Android O
中运行良好,但在 Android P
中,有时焦点在 PopupMenu 中丢失
项目,直到重新启动系统才重新获得焦点.使用后dispatchKeyEvent
并将动态焦点设置为 v.requestFocus();
应用程序开始工作得更好一些,但有时仍会观察到警告,感觉就像系统挂起给用户.查看以下代码片段以了解详细信息:
Note: Same Application is working fine in Android O
, but in Android P
, sometimes the focus is missing in PopupMenu
items and not getting back the focus until reboot the system. After using
dispatchKeyEvent
and setting dynamically focus to the view as v.requestFocus();
the application started working little better but still the warning is observed sometimes with feel like system hanged for a user. Check below code snippet for details:
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
LogUtils.d(TAG, String.valueOf(event.getKeyCode()));
this.getWindow().getDecorView().setFocusable(true);
return super.dispatchKeyEvent(event);
}
我的问题是,如何将缺少的焦点动态设置到 PopupMenu
和 RecyclerView
或如何在 Android 中运行的应用程序中克服这个问题
My Question is, how to set the missing focus dynamically to the PopupMenu
and RecyclerView
or how to overcome this in application running in Android P.
请提出建议,我在 stackOverflow 中遇到了相同类型的问题,但不是正确的解决方案,因此发布问题并等待所有类型的建议和答案.提前致谢.
Please suggest, I got same type of question in stackOverflow but not a proper solution, so posting the question and waiting for all type of suggestion and answer. Thanks in advance.
推荐答案
在我的例子中,类似的问题是由在任何地方使用 onKeyDown() 和 KeyEvent.ACTION_DOWN 引起的,包括在按键时显示对话框时.使用 onKeyUp() 和 KeyEvent.ACTION_UP 显示对话框和活动为我解决了这个问题.
In my case similar issue was caused by using onKeyDown() and KeyEvent.ACTION_DOWN everywhere, including when showing a dialog on a key press. Using onKeyUp() and KeyEvent.ACTION_UP to show dialogs and activities resolved it for me.
这篇关于在 Android P 中:由于没有窗口焦点而丢弃事件:KeyEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!