如何在 Wear OS 的手表界面/应用中请求当前偏好屏幕的焦点?

huangapple go评论43阅读模式
英文:

For a Wear Os watchface/app, how can I request focus for the current preference screen?

问题

我有一个 Wear OS 应用(表盘),用户可以打开一个首选项活动(基于 PreferenceFragment 的 PreferenceScreen),用于设置用户设置。我正在尝试启用手表旋钮或旋钮边框的旋转输入,但尚未成功。

到目前为止,我发现问题与缺少焦点有关。那么,在首选项屏幕创建时请求焦点的正确方式是什么?

英文:

I have a Wear OS app (watch face) where the user can open a preference activity (PreferenceScreen based on PreferenceFragment) for all the user settings. I am trying to enable rotary input for the watch knob or rotary bezel, but haven't been able to.

So far I found out that the issue is related with missing focus. So, what is the proper way to request focus when the preference screen is created?

答案1

得分: 0

最终我找到了解决方案:在我的PreferenceFragment中,我必须重写onActivityCreated并在当前视图上请求焦点。(似乎在使用PreferenceFragmentCompat时也有效)。

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (getView() != null) getView().requestFocus(); // 启用旋钮输入
}

唯一的问题是,当打开首选屏幕中的嵌套子部分时,旋钮输入会停止。但在返回时会恢复。这似乎是正常的,如https://developer.android.com/training/wearables/user-input/rotary-input中所述:“如果您的活动包含多个可滚动视图,请选择一个视图使用<requestFocus />标记来进行焦点设置。不支持旋转侧按钮的嵌套滚动”。

英文:

Finally I found a solution: in my preferenceFragment I had to override onActivityCreated and request the focus on the current view. (Seems to also work when using preferenceFragmentCompat).

        @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getView() != null) getView().requestFocus(); // ENABLES ROTARY INPUT
    }

The only thing is that rotary input stops when opening a nested subsection in the preference screen. But it reestablishes when coming back. This seems to be normal as described in https://developer.android.com/training/wearables/user-input/rotary-input: "If your activity contains multiple scrollable views, choose one to focus using the &lt;requestFocus /&gt; tag. Nested scrolling is not supported with the rotating side button."

huangapple
  • 本文由 发表于 2023年6月22日 10:34:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定