我的活动中有一个复选框,并提供了android:contentDescription="selected"。同样在java类中提供,如下所示。
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
checkbox.setContentDescription(b ? "Selected" : "Not Selected");
}
});当我打开对讲功能并选中复选框时,它显示“已选中/未选中”而不是“已选中/未选中”。
它采用操作系统的默认值(在不同的制造商和操作系统版本中有所不同),但不提供值。有没有办法,我们可以解决这个问题?
发布于 2020-11-19 19:01:58
所以我在一段时间之前遇到了这个问题,并找到了一个相当棘手的解决方法。创建并使用CheckBox的子类,如下所示,并替换字符串:
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中
/**
* 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);
}
}https://stackoverflow.com/questions/54161090
复制相似问题