首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当辅助功能打开时,如何更改/覆盖复选框内容描述值?

当辅助功能打开时,如何更改/覆盖复选框内容描述值?
EN

Stack Overflow用户
提问于 2019-01-12 23:37:58
回答 1查看 665关注 0票数 1

我的活动中有一个复选框,并提供了android:contentDescription="selected"。同样在java类中提供,如下所示。

代码语言:javascript
复制
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        checkbox.setContentDescription(b ? "Selected" : "Not Selected");
    }
});

当我打开对讲功能并选中复选框时,它显示“已选中/未选中”而不是“已选中/未选中”。

它采用操作系统的默认值(在不同的制造商和操作系统版本中有所不同),但不提供值。有没有办法,我们可以解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2020-11-19 19:01:58

所以我在一段时间之前遇到了这个问题,并找到了一个相当棘手的解决方法。创建并使用CheckBox的子类,如下所示,并替换字符串:

代码语言:javascript
复制
public class CustomCheckBox extends CheckBox {

    // constructors...

    @Override
    public CharSequence getAccessibilityClassName() {
        // override to disable the "checkbox" readout
        return CustomCheckBox.class.getSimpleName();
    }

    @Override
    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
        super.onInitializeAccessibilityNodeInfo(info);
        // by setting checkable to false the default checked/unchecked readouts are disabled
        info.setCheckable(false);
        // ...and then you can set whatever you want as a text
        info.setText(getStateDescription());
    }

    @Override
    public void setChecked(boolean checked) {
        if (checked == isChecked()) return;
        super.setChecked(checked);
        // since we've disabled the checked/unchecked readouts
        // we are forced to manually announce changes to the state
        announceForAccessibility(getStateDescription());
    }

    private String getStateDescription() {
        if (isChecked()) {
            return "Custom checked description";
        } else {
            return "Custom unchecked description";
        }
    }
}

另外,我应该说我还没有尝试过下面提到的东西,但似乎Android R(API30)添加了一种官方方法来覆盖它,将setStateDescription(CharSequence)添加到CompoundButton Source Doc

代码语言:javascript
复制
/**
 * This function is called when an instance or subclass sets the state description. Once this
 * is called and the argument is not null, the app developer will be responsible for updating
 * state description when checked state changes and we will not set state description
 * in {@link #setChecked}. App developers can restore the default behavior by setting the
 * argument to null. If {@link #setChecked} is called first and then setStateDescription is
 * called, two state change events will be merged by event throttling and we can still get
 * the correct state description.
 *
 * @param stateDescription The state description.
 */
@Override
public void setStateDescription(@Nullable CharSequence stateDescription) {
    mCustomStateDescription = stateDescription;
    if (stateDescription == null) {
        setDefaultStateDescritption();
    } else {
        super.setStateDescription(stateDescription);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54161090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档