ReactNative中的可访问性标识符问题

时间:2020-01-03 08:19:38

标签: android ios react-native accessibility

我正在使用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,

我不知道代码:(

1 个答案:

答案 0 :(得分:0)

There was a bug具有在0.60.5中修复的react native。

我的猜测是您使用的是早期版本(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中的更改替换文件。