2

私はAndroidランチャーアプリケーションに取り組んでいます。AppWidgetsをホストして、LinearLayoutから派生したビューに表示できます。デフォルトのランチャーの場合のように、ウィジェット内のボタン(たとえば、Power Controlウィジェットの個々のコントロール)にフォーカスを設定する方法がわかりません。

これが、AppWidgetHostViewにフォーカスを割り当てる方法です。

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

ウィジェット全体に焦点が当てられます。十字キーのEnterボタンをクリックするとウィジェット全体が点滅するので、これを知っています。

ウィジェット内のボタンの1つにフォーカスを設定するにはどうすればよいですか?ウィジェットのRemoteViewを列挙する必要がありますか?もしそうなら、どのように?

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 に答える