我正在使用 iOS 中的 testID
和 android 中的 accessibilityLabel
为 React Native 中的组件设置可访问性标识符。对于 iOS 来说工作正常,但是对于 Android 来说,我的标识符附加了 ,
(一个逗号和一个空格)。我不确定是什么引起了问题。这是我的代码:
const renderAccessibilityLabel = (str) => {
const propsForAutomation = {};
if (Platform.OS === "ios") {
propsForAutomation.testID = str;
} else {
propsForAutomation.accessibilityLabel = str;
}
return propsForAutomation;
};
// Inside render method:
<Text {...renderAccessibilityLabel("MyText")}>{MyText}</Text>
结果> ios: MyText
android: MyText,
我不知道代码有什么问题:(
最佳答案
There was a bug使用 React Native,该问题已在 0.60.5 中修复。
我的猜测是您使用的是早期版本 (0.60),更新将纠正此问题。
问题出现在ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java
第 178 行 contentDescription.append(accessibilityLabel + ", ");
万一您因任何原因无法更新,可以通过在 行上方添加
contentDescription = contentDescription.replaceAll(", $", "");
来修复此问题if (contentDescription.length() > 0) {
或者通过将文件替换为 https://github.com/facebook/react-native/commit/812abfd 中的更改.
关于android - React Native 中的可访问性标识符问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59575216/