标签: touchableopacity

React Native 防止触摸冒泡到父元素

如果我想防止View组件上的 onPress 事件传播到Touchable以下Sample组件的父级,除了将子视图包装在一个中之外,最好的选择是Touchable什么?

export default function Sample (): Element<*> {
  return(
    <TouchableOpacity>
      <View>
        <Text>Sample</Text>
      </View>
    </TouchableOpacity>
  );
}
Run Code Online (Sandbox Code Playgroud)

react-native react-native-android react-native-ios touchableopacity touchablehighlight

6
推荐指数
1
解决办法
2195
查看次数

滚动时禁用TouchableOpacity突出显示

我想在FlatList或ScrollView中滚动时禁用TouchableOpacity的突出显示.滚动时onPress没有触发,它只是被触发的突出显示效果.

我尝试过delayPressIn但它只延迟onPress时间而不是突出显示效果.

react-native touchableopacity react-native-flatlist

6
推荐指数
1
解决办法
1156
查看次数

如何正确使用 React Native onLongPress?

我有一段简单的代码,它只是一个带有 onLongPress 道具的 TouchableOpacity,但它似乎不起作用。

<TouchableOpacity delayLongPress={10} onLongPress={()=>{console.log("pressed")}} activeOpacity={0.6}>
  <Text>BUTTON</Text>
</TouchableOpacity>
Run Code Online (Sandbox Code Playgroud)

我试过删除延迟道具,但这仍然不起作用。然而,将 onLongPress 更改为 onPress 似乎确实有效,但我想要长按功能。我正在 Android 模拟器上对此进行测试。

react-native react-native-android touchableopacity touchablehighlight touchablewithoutfeedback

6
推荐指数
1
解决办法
1万
查看次数

为什么 hitSlop 在这个嵌套的 TouchableOpacity 组件中不起作用?

我有一个嵌套的 TouchableOpacity 组件,如下所示:

  <View style={styles.mainTextContainer}>
    <Text>
      {mainText}
      <TouchableOpacity
        hitSlop={{top: Gutter, left: Gutter, bottom: Gutter, right: Gutter}}
        style={styles.icon}
        onPress={onPress}>
        {mainTextIcon}
      </TouchableOpacity>
    </Text>
  </View>

  // These are the corresponding styles
  icon: {
    justifyContent: "center",
  },
  mainTextContainer: {
    flexDirection: "row",
    flexWrap: "wrap",
    justifyContent: "flex-start",
  },
Run Code Online (Sandbox Code Playgroud)

但是, hitSlop 道具似乎不起作用。是因为它嵌套在文本组件内吗?

react-native touchableopacity

6
推荐指数
0
解决办法
774
查看次数

React native TouchableOpacity onPress问题

我有一个简单的图标按钮如下:

class SideIcon extends Component {
  render() {
    return (
      <TouchableOpacity onPress={console.log('puff')} style={styles.burgerButton}>
        <Icon name="bars" style={styles.burgerIcon}/>
      </TouchableOpacity>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

它来自以下组件:

export default test = React.createClass({
  getInitialState: function() {
    return {
      isModalOpen: false
    }
  },

  _openModal() {
    this.setState({
      isModalOpen: true
    });
  },

  _closeModal() {
    this.setState({
      isModalOpen: false
    });
  },
  render() {
    return (
     <View style={styles.containerHead} keyboardShouldPersistTaps={true}>
     **<SideIcon openModal={this._openModal} closeModal={this._closeModal} />**
      <Text style={styles.logoName}>DareMe</Text>
      <SideBar isVisible={this.state.isModalOpen} />
     </View>
    );
  }
});
Run Code Online (Sandbox Code Playgroud)

但onPress 对TouchableOpacity 从未工程.我哪里错了?虽然它在组件加载期间显示控制台语句.

react-native touchableopacity

5
推荐指数
1
解决办法
2万
查看次数

使用 TouchableOpacity 而不是 TouchableWithoutFeedback 时样式会中断

这是使用 TouchableWithoutFeedback 时这部分的外观

这是同样的事情,除了 TouchableOpacity

这部分的代码:

<TouchableWithoutFeedback>
    <View style={styles.buyCoinsItem}>
        <View style={styles.cost}>
            <Text>{no_of_coins}</Text>
        </View>
        <Text>{product.priceString}</Text>
        <View style={{height:30, flexDirection: 'row', marginTop:10}}>
            {displayDiscount && 
            <View style={styles.discountContainer}>
                <Text style={styles.whiteText}>Save {discount}</Text>
            </View>
            }
        </View>
    </View>
</TouchableWithoutFeedback>
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?

react-native touchableopacity

5
推荐指数
1
解决办法
2127
查看次数

如何根据其中的组件调整TouchableOpacity的大小?

我使用TouchableOpacity,使一个按钮,我反应过来学习母语。

但是问题是,TouchableOpacity占用屏幕的100%宽度。但是,我要考虑其中包含的组件的大小/增长。

我该怎么做?

import React, { Component } from "react";
import { Text, TouchableOpacity } from "react-native";
export default class App extends Component {
    render(){
        return(
        <TouchableOpacity
            onPress={() => console.log("Pressed!")}
            style={{ backgroundColor: "red" }}
        >

            <Text>Press me!</Text>

        </TouchableOpacity>
        );
    }
Run Code Online (Sandbox Code Playgroud)

当我减小TouchableOpacity10或20 之类的宽度时,它会自动增加其高度以适合Text。

那么,是否有可能TouchableOpacity随着内部组件的大小而增长?

javascript react-native touchableopacity

5
推荐指数
1
解决办法
2514
查看次数

反应原生 TouchableOpacity 不适用于里面的图标

我有一个项目,我想在其中添加一个带有图标“+”的按钮,但是可触摸的不透明度不能作为按钮使用,我单击它,并且不产生不透明度,然后我 console.log() , 查看按钮是否正常工作,只有不透明度反馈是问题,但效果不佳。

这是我的渲染:

render() {
    if (this.props.onNetwork)
        return (<Loader />);

    const navigate = this.props.navigation.navigate;
    const { solicitacoes } = this.props.solicitacoesStore;

    return (
        <View style={styles.container}>

            <View style={{flexDirection:'row', justifyContent:'space-between'}}>
              <H2 style={styles.header2}>PROJETOS</H2>
              <TouchableOpacity onPress={() => { console.log('puff') }}>
                <Icon style={styles.icon} name='add' />
              </TouchableOpacity>
            </View>
            <Divider color='black'/>

            <ListaItens solicitacoes={solicitacoes} onPress={ this.goToDescritivo }/>
        </View>
    )
}

    const styles = StyleSheet.create({
container: {
    flex: 1,
    backgroundColor: '#FFFFFF',
    paddingHorizontal: metrics.width * 0.05,
    paddingTop: metrics.height * 0.03
},

icon: {
    fontSize: fonts.superbig * 1.5,
    flex:1
}, …
Run Code Online (Sandbox Code Playgroud)

feedback reactjs react-native touchableopacity

5
推荐指数
0
解决办法
4155
查看次数

带有TouchableOpacity onpress的图像无法在本机中运行

我是新来响应本地开发的人。在一个屏幕上的应用程序中,我单击了图像。因此,我在可触摸的不透明度内拍摄了图像,并为该可触摸的不透明度写了onpress方法。但是onpress无法正常工作。但是我已经将相同的代码写到了另一张图像,但是这个正在工作。

这是代码

mport React, {Component} from 'react';
import {Text, View,TouchableHighlight,TouchableOpacity,StyleSheet,StatusBar,Image} from 'react-native';

export default class Profile extends Component {

    static navigationOptions = ({ navigation }) => {
      return {
        title: `Welcome`,
        header: null,
      }
    };

    render() {
      const { state, navigate } = this.props.navigation;
      return (
        <View style={styles.MainContainer}>
        <StatusBar
          barStyle="light-content"
          backgroundColor="#2f5597"
        />
             <View style={{backgroundColor: "#fff",padding: 10}}>
                 <Image source={require('../../app/images/ic_visa.png')} style={{marginLeft: -10}}></Image>
                 <TouchableOpacity  onPress={() => this.backArrow()} style={styles.signoutContainer}> 
                    <Image source={require('../../app/images/ic_Signout.png')} style={styles.signOutImage}></Image>
                 </TouchableOpacity>
             </View>

             <View style={{backgroundColor: "#2f5597",flexDirection: 'row',alignItems: 'center'}}>
                <TouchableOpacity onPress={() => this.backArrow()} style={{padding: …
Run Code Online (Sandbox Code Playgroud)

image react-native touchableopacity onpress

5
推荐指数
1
解决办法
2609
查看次数

React Native 不触发 Console.log

我有这个小样本反应本机代码,代表我的原始代码的主要功能。我正在尝试 console.log TouchbleOpacity onPress()。但该函数不会在控制台上触发任何内容。此外,组件安装后它不会立即进行控制台。警报工作正常,但这并不重要。这是我的代码:

import React, { Component } from 'react'
import { Text, View, TextInput, TouchableOpacity } from 'react-native'

export default class Terms extends Component {

  componentDidMount() {
    console.log('component mounted');
  }

  submitData = () => {
    console.log('button pressed');
    alert('hiiiii')
  }


  render() {
    return (
      <View>

        <TouchableOpacity onPress = {this.submitData} >
        <Text> textInComponent </Text>
        </TouchableOpacity>
      </View>
    )
  }
}
Run Code Online (Sandbox Code Playgroud)

我也尝试了不同的函数绑定方法,但无法找出问题所在。以前似乎工作得很好,但现在不行了。请帮助修复。

javascript console react-native touchableopacity

5
推荐指数
1
解决办法
6595
查看次数