AppWidgetHost 如何将焦点设置到 AppWidget 按钮?

发布于 2024-10-20 08:55:18 字数 404 浏览 1 评论 0原文

我正在开发 Android 启动器应用程序。我可以托管 AppWidget 并将它们显示在我的视图上,该视图派生自 LinearLayout。我不知道如何将焦点设置到小部件内的按钮(例如,电源控制小部件的各个控件),就像默认启动器一样。

这就是我将焦点分配给 AppWidgetHostView 的方式:

widgetView.setFocusable(true);
widgetView.setSelected(true);
widgetView.requestFocus(true);

整个小部件都有焦点。我知道这一点是因为如果我单击 dpad Enter 按钮,整个小部件都会闪烁。

如何将焦点设置到小部件内的按钮之一?我需要枚举小部件的 RemoteViews 吗?如果是这样,怎么办?

I am working on an Android Launcher application. I can host AppWidgets and display them on my View, which is derived from a LinearLayout. I can't figure out how to set focus to the buttons within the widget (for example, the individual controls of the Power Control widget), as happens with the default launcher.

This is how I am assigning the focus to the AppWidgetHostView:

widgetView.setFocusable(true);
widgetView.setSelected(true);
widgetView.requestFocus(true);

The entire widget has the focus. I know this because the entire widget flashes if I click the dpad enter button.

How do I set the focus to one of the buttons within the widget? Do I need to enumerate the RemoteViews of the widget? If so, how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

简单爱 2024-10-27 08:55:18

我想通了。 (谢谢 HierarchyViewer)。如果AppWidgetHostView的第一个子项是ViewGroup(例如LinearLayout),我会从小部件中删除焦点并使用FocusFinder将焦点分配给第一个子项。

View view = getView(); // from AppWidgetHost().createView
ViewGroup hostView;
if ( !(view instanceof ViewGroup) )
    return;             // hostView does not a child widget!
hostView = (ViewGroup) view;
View widget = hostView.getChildAt(0);
if ( !(widget instanceof ViewGroup) ) 
    return;             // widget does not have children
hostView.setFocusable(false);
hostView.setSelected(false);

// select first child
FocusFinder focusFinder = FocusFinder.getInstance();
View nextView = focusFinder.findNextFocus(hostView, null, View.FOCUS_RIGHT);
if (nextView != null) {
    nextView.setSelected(true);
    nextView.requestFocus();
}

I figured it out. (Thank you HierarchyViewer). If the AppWidgetHostView first child is a ViewGroup (e.g. LinearLayout), I remove focus from the widget and use the FocusFinder to assign focus to the first child.

View view = getView(); // from AppWidgetHost().createView
ViewGroup hostView;
if ( !(view instanceof ViewGroup) )
    return;             // hostView does not a child widget!
hostView = (ViewGroup) view;
View widget = hostView.getChildAt(0);
if ( !(widget instanceof ViewGroup) ) 
    return;             // widget does not have children
hostView.setFocusable(false);
hostView.setSelected(false);

// select first child
FocusFinder focusFinder = FocusFinder.getInstance();
View nextView = focusFinder.findNextFocus(hostView, null, View.FOCUS_RIGHT);
if (nextView != null) {
    nextView.setSelected(true);
    nextView.requestFocus();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文