如何在touchableopacity中设置焦点?

时间:2020-05-18 11:13:07

标签: react-native

我正在为android-TV开发应用程序,我需要将重点放在TouchableOpacity上。 我怎样才能做到这一点? 我在Android TV上进行焦点检测的示例

enableTVEventHandler() {
    this.evtHandler = new TVEventHandler();
    this.evtHandler.enable(this, this.handleTVRemoteEvent);
}


disableTVEventHandler() {
    if (this.evtHandler) {
        this.evtHandler.disable();
        delete this.evtHandler;
    }
}

handleTVRemoteEvent = (cmp, event) => {
    const { eventType, tag } = event;

    if (tag !== this.nodeHandle) {
        return;
    }

    if (eventType === "focus") {
        this.setState({ isFocus: true });

        if (this.props.focus !== undefined) {
            this.props.focus();
        }
    }

    if (eventType === "blur") {
        this.setState({ isFocus: false });
    }
}

1 个答案:

答案 0 :(得分:0)

工作了一段时间后,我找到了这个解决方案:

<TouchableHighlight
  onPress={this.props.onPress}
  onFocus={() => {
    this.setState({
      currentStyle: 'blue',
    })
  }}
  onBlur={() => {
    this.setState({
      currentStyle: 'white',
    })
  }}
>
{this.props.children}
</TouchableHighlight>