android - React Native 中的可访问性标识符问题

标签 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,

我不知道代码有什么问题:(

最佳答案

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/

相关文章:

未设置 React-Native Keystore 文件用于签署配置发布

android - react-native-video 在 Android 背景下崩溃

android - Phonegap - 如何将 .txt 文件保存在 android 手机的根目录中

java - 为什么 ViewPager 很慢

android - OpenCv 与 Android studio 2.2+ 使用带有 cmake 的新 gradle - 未定义引用

iphone - 无法复制 App Sandbox 中的项目

android - 如何在android中更改EditText提示颜色

ios - CocoaPods Swift 链接器错误

objective-c - 如何在 UITextfield 中输入时和输入后放置货币符号?

android - React-Native: "FirebaseError: No Firebase App ' [默认 ]' has been created - call Firebase App.initializeApp() (app/no-app)"