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

huangapple go评论87阅读模式
英文:

Accessibility Identifier issue in ReactNative

问题

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;
};

// 在渲染方法中使用:
<Text {...renderAccessibilityLabel("MyText")}>{MyText}</Text>

结果:
iOS:MyText
Android:MyText,
我不知道代码有什么问题 ReactNative中的可访问性标识符问题

英文:

I am setting accessibility identifier for my components in react native using testID in iOS and accessibilityLabel in android. For iOS is working fine but for Android it my identifier is appended with , (a comma and a space). I am not sure what is causing issue. Here is my code:

const renderAccessibilityLabel = (str) =&gt; {
  const propsForAutomation = {};
  if (Platform.OS === &quot;ios&quot;) {
    propsForAutomation.testID = str;
  } else {
    propsForAutomation.accessibilityLabel = str;
  }
  return propsForAutomation;
};

// Inside render method:
&lt;Text {...renderAccessibilityLabel(&quot;MyText&quot;)}&gt;{MyText}&lt;/Text&gt;

result > ios: MyText
android: MyText,

I don't know whats wrong with code ReactNative中的可访问性标识符问题

答案1

得分: 0

有一个在 React Native 中的 bug,已在 0.60.5 版本中修复。

我猜测你正在使用一个较早的版本 (0.60),更新版本将会纠正此问题。

问题出现在 ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java 文件中。

第 178 行:contentDescription.append(accessibilityLabel + &quot;, &quot;);

如果因某种原因无法更新,可以通过在 if (contentDescription.length() &gt; 0) { 行之前添加 contentDescription = contentDescription.replaceAll(&quot;, $&quot;, &quot;&quot;); 来修复。

或者,可以替换该文件以包含此更改:https://github.com/facebook/react-native/commit/812abfd

英文:

There was a bug with react native that was fixed in 0.60.5.

My guess is you are using an earlier version (0.60), updating will correct this.

Problem occurs in ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java

Line 178 contentDescription.append(accessibilityLabel + &quot;, &quot;);

Just in case you can't update for any reason, it can be fixed by adding contentDescription = contentDescription.replaceAll(&quot;, $&quot;, &quot;&quot;); just above the line if (contentDescription.length() &gt; 0) {

Or by replacing the file with the changes in https://github.com/facebook/react-native/commit/812abfd.

huangapple
  • 本文由 发表于 2020年1月3日 16:19:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定