什么是AccessibilityInfo.setAccessibilityFocus(reactTag)方法中的reactTag参数? React native documentation不提供有关此参数的任何信息:
Set accessibility focus to a React component. On Android, this is equivalent
to UIManager.sendAccessibilityEvent(reactTag,
UIManager.AccessibilityEventTypes.typeViewFocused);.
我没有Objective-C和Java的背景.一个小例子将不胜感激.谢谢 !!!
解决方法:
reactTag只是React用来标识应用程序中的视图对象的数字.它是findNodeHandle函数的结果,该函数将视图引用作为参数.
这是一个有关如何使用它的简单示例:
import React, {Component} from 'react'
import {
...
findNodeHandle,
...
} from 'react-native';
class Sample extends React.Component {
constructor(props) {
super(props)
this.viewRef = null;
}
...
componentDidMount() {
if (this.viewRef) {
const reactTag = findNodeHandle(this.viewRef);
AccessibilityInfo.setAccessibilityFocus(reactTag);
}
}
render() {
return (
<View ref={el => { this.viewRef = el }}>
...
</View>
)
}
}