reactjs - 如何在 React Native android 上制作一个包裹相机可点击的 TouchableOpacity?
问题描述
我在 react-native TouchableOpacity 中渲染一个相机视图,如何让它可点击?
相同版本的代码在 iOS 上运行良好,touchableOpacity 是可点击的并产生正确的输出
<TouchableOpacity style={{width:'100%', height:300}} onPress={() =>alert("hey")}>
<Camera
style={{ height: 300, width: '100%', display: this.state.camera }}
type={this.state.type}
autoFocus={'on'}
ratio={'4:3'}
focusDepth={0}
ref={(ref) => { this.camera = ref }}>
</Camera>
</TouchableOpacity>
当我按下 TouchableOpacity 时,我希望输出是一个带有“嘿”的警报,而不是我在 android 上什么都没有,但我在 iOs 上得到一个“嘿”
解决方案
这是因为 TouchableOpacity 行为在 iO 和 Android 之间存在差异。一个快速的解决方法就是在 Android 中用 TouchableWithoutFeedback 替换 TouchableOpacity。这是一种方法:
const Touchable = Platform.select({ ios: TouchableOpacity, android: TouchableWithoutFeedback });
然后只需使用此常量来包装您的相机视图。
PS:确保你从 react-native 模块导入了 TouchableOpacity、TouchableWithoutFeedback 和 Platform。
推荐阅读
- json - 如何将列表转换为 JSON?
- reactjs - 无法调用 React ExoticComponent
- kubernetes - Kubernetes 无法访问同一命名空间内的 Cassandra
- wordpress - 无论如何我可以在 web.config 文件中将网站从 .co.uk 更改为 .com
- shiny - 减少 TextInput 小部件中的输入
- python - 在python中将CSV循环导入无穷大
- reactjs - React 应用程序是如何构建的?
- python - 将 c_byte-array 转换为不同的类型
- java - 如何从firebase检索数据并将其放入文本字段
- node.js - 如何使用 CircleCI 进行语义发布在未发现任何更改时失败