【问题标题】:How does an AppWidgetHost set focus to an AppWidget button?AppWidgetHost 如何将焦点设置到 AppWidget 按钮?
【发布时间】:2011-03-04 18:57:05
【问题描述】:

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

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

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

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

如何将焦点设置到小部件中的某个按钮?我需要枚举小部件的 RemoteViews 吗?如果有,怎么做?

【问题讨论】:

    标签: android android-widget android-appwidget


    【解决方案1】:

    我想通了。 (谢谢 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();
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      • 2021-05-21
      • 2015-03-19
      相关资源
      最近更新 更多