【问题标题】:How to give contentDescription to SwitchPreference for Accessibilty/Talkback?如何为无障碍/对讲的 SwitchPreference 提供内容描述?
【发布时间】:2020-07-27 16:52:53
【问题描述】:

我有一个具有多个 SwitchPreferences 的首选项屏幕。我需要为每个开关提供自定义 contentDescription 以实现可访问性。 SwitchPreference 没有作为 contentDescription 的属性。任何人都可以帮助我如何为 Accessibilty/Talkback 的 switchPreferences 提供自定义描述吗?

【问题讨论】:

  • 请向我们展示您目前的工作/代码

标签: android kotlin android-preferences preferencescreen switchpreference


【解决方案1】:

首选项不是视图。您不能直接将contentDescription 添加到首选项。您需要创建一个从SwitchPreference 扩展的自定义首选项,并将可访问性信息添加到onBindViewHolder 中的切换视图。

public class SwitchPreferenceAccessibility extends SwitchPreference {

  @Override
  public void onBindViewHolder(PreferenceViewHolder holder) {
    super.onBindViewHolder(holder);
    
    View switchView = holder.findViewById(R.id.switchWidget);
    switchView.setContentDescription(getString(R.string.accessibility_information));
  }
}

或者你可以使用declare-styleable属性,这样会更方便。

public class SwitchPreferenceAccessibility extends SwitchPreference {

  public SwitchPreferenceAccessibility(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);

    mContentDescription = TypedArrayUtils.getText(a, R.styleable.SwitchPreferenceAccessibility_content_description, "");
  }

  @Override
  public void onBindViewHolder(PreferenceViewHolder holder) {
    super.onBindViewHolder(holder);
    
    View switchView = holder.findViewById(R.id.switchWidget);
    switchView.setContentDescription(mContentDescription);
  }
}
<SwitchPreferenceAccessibility
  app:key="key"
  app:title="title"
  app:content_description="@string/accessibility_information" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 2011-07-27
    • 2017-10-01
    • 1970-01-01
    相关资源
    最近更新 更多