2

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

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

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

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

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

4

1 回答 1

3

我想到了。(谢谢 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();
}
于 2011-03-10T19:23:25.893 回答