简体   繁体   English

如何通过点击可触摸组件来触发另一个可触摸组件的按下事件?

[英]How to trigger another Touchable component's press event by tap a touchable component?

How to trigger another Touchable component's press event by tap a touchable component?如何通过点击可触摸组件来触发另一个可触摸组件的按下事件? thanks!谢谢!

Example:-例子:-

<TouchableHighlight onPress={this._triggerSecond}>
  <Image
    style={styles.button}
    source={require('./myButton.png')}
  />
</TouchableHighlight>

If user will tap on first TouchableHighlight then it should trigger second TouchableHighlight.如果用户点击第一个 TouchableHighlight,那么它应该触发第二个 TouchableHighlight。

<TouchableHighlight>
  <Image
    style={styles.button}
    source={require('./myButton.png')}
  />
</TouchableHighlight>

For those who still need to fire a press programmatically, I found a solution.对于那些仍然需要以编程方式触发印刷机的人,我找到了一个解决方案。 First you need to bind your touchable to this by using ref :首先,您需要使用 ref 将您的可触摸对象绑定到this

<TouchableOpacity
  onPress={() => alert("click")}
  ref={(touchable) => this._touchable = touchable}
>

then when you want to launch onPress without clicking it just call然后当你想在不点击的情况下启动 onPress 时,只需调用

this._touchable.touchableHandlePress()

Well I guess instead of thinking in that way I guess you can thinking this as just another TouchableHighlight calling the same method such as:好吧,我想而不是那样想,我想您可以将其视为调用相同方法的另一个TouchableHighlight ,例如:

// First one 
<TouchableHighlight onPress={this._method}>
  <Image
    style={styles.button}
    source={require('./myButton.png')}
  />
</TouchableHighlight>

// Second one
<TouchableHighlight onPress={this._method}>
  <Image
    style={styles.button}
    source={require('./myButton.png')}
  />
</TouchableHighlight>

In this way both components when tap are going to invoke to the same method .这样,当 tap 时两个组件将调用相同的method

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 React-Native 中从内部 Touchable 到外部 Touchable 冒泡新闻事件? - How to Bubbling press event from inner Touchable to outer Touchable in React-Native? 监听可触摸元素内的新闻事件的元素 - Element that listen press event inside touchable element 如何在Touchable组件中包含ScrollView? - How can I include a ScrollView inside a Touchable component? 在React Native中对齐Touchable组件的内部标签 - Align inner tags of Touchable component in React Native 当按下另一个可触摸的不透明度时,一个可触摸的不透明度不会按下 - One touchable opacity doesn't press when another touchable opacity is pressed 如何从另一个可触摸/可按下的按钮中按下按钮? [反应原生] - How can I press a button from within another touchable/pressable? [React Native] 按下时未触发可触摸的不透明度 - touchable opacity not triggered when press on it 错误:可触摸子项必须是本机或将 setNativeProps 转发到本机组件 - ERROR: Touchable child must either be native or forward setNativeProps to a native component 如何解决自定义抽屉组件可触摸 onPress 屏幕切换在 React-Native 中不起作用? - How to solve Custom Drawer Component Touchable onPress Screen switching not Working in React-Native? 如何在 Touchable press 上重新渲染 React Native FlatList? - How to re-render React Native FlatList on Touchable press?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM