选项菜单 - 从项目中删除焦点

发布于 2024-09-27 15:32:44 字数 401 浏览 1 评论 0原文

如何从选项菜单项中删除焦点?即,当我第一次打开菜单时,没有任何项目具有焦点。但是,如果我使用轨迹球专注于其中一个,然后关闭并重新打开菜单,焦点仍然存在。我该如何摆脱它?

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反目相谮 2024-10-04 15:32:44

使用轨迹球(或者确实按下键盘上的任何其他键)后,您将退出 触摸模式。从那时起,将会有一些视图获得焦点(您可以通过调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文