选项菜单 - 从项目中删除焦点
如何从选项菜单项中删除焦点?即,当我第一次打开菜单时,没有任何项目具有焦点。但是,如果我使用轨迹球专注于其中一个,然后关闭并重新打开菜单,焦点仍然存在。我该如何摆脱它?
我正在 onPrepareOptionsMenu
中清除并重新创建菜单(因为我必须将其调整到当前活动状态)。
编辑:
public boolean onPrepareOptionsMenu(Menu menu){
menu.clear();
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return true;
}
how do I remove focus from options menu item? I.e. when I open the menu for the first time, none of the items has focus. however, if I focus on one of them using track ball, and then close and re-open the menu the focus is still there. How do I get rid of it?
I am clearing and recreating the menu in onPrepareOptionsMenu
(as I have to adjust it to the current activity state).
EDIT:
public boolean onPrepareOptionsMenu(Menu menu){
menu.clear();
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用轨迹球(或者确实按下键盘上的任何其他键)后,您将退出 触摸模式。从那时起,将会有一些视图获得焦点(您可以通过调用 Activity.getCurrentFocus() 来查看这一点)。此模式一直持续到您再次触摸屏幕为止,此时您将重新进入触摸模式,并且当您随后打开菜单时,将不会有焦点项目。我见过的带有菜单的每个 Android 应用程序都会发生这种情况。
这种情况不仅存在于选项菜单项,也存在于布局中的视图。一旦你离开触摸模式,我认为没有办法再次进入它(从而从所有视图中移除焦点),除非你触摸屏幕。我见过的一个用于消除焦点可见效果(不在触摸模式下)的解决方案是简单地将其赋予不可见的元素。您可以创建一个不可见的菜单选项并在您想要清除它时为其提供焦点吗?
除非您找到一种方法以编程方式可靠地进入触摸模式或从所有视图中删除焦点(调用 View.clearFocus() 只是将其传递到另一个视图!),否则这可能是您最好的选择。
Once you use the trackball (or indeed press any other key on the keyboard), you exit touch mode. From that point on, there will be some view that has focus (you can see this by calling
Activity.getCurrentFocus()
). This mode persists until you touch the screen again, at which point you will re-enter touch mode and when you open the menu on subsequent occasions there will not be a focused item. This happens in every single Android application I have seen with a menu.This situation exists not only for option menu items, but also for views in your layout. Once you leave touch mode, I don't think there is a way to enter it again (and hence remove focus from all views) unless you touch the screen. A solution I have seen cited for removing the visible effects of focus (when not in touch mode) is to simply give it to an element which is not visible. Could you create an invisible menu option and give it focus when you want to clear it?
Unless you find a way to programmatically, reliably enter touch mode or to remove focus from all views (calling
View.clearFocus()
just passes it to another view!), this may be your best bet.