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

时间:2018-10-16 12:06:58

标签: image react-native 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: 12.5,justifyContent: 'center',alignItems: 'center', backgroundColor:"#2f5597"}}>
                   <Image source={require('../../app/images/ic_arrow_white.png')} style={{justifyContent: 'flex-start',alignSelf: 'center',alignContent: 'center'}}></Image>
                </TouchableOpacity>
                <Text style={{padding: 10,color:'#fff', backgroundColor:"#2f5597", fontSize: 18,fontWeight: "bold", paddingLeft: 5,alignSelf: 'center'}}>Confirm payment</Text>
                <View style={styles.signoutContainer}> 
                    <Text style={{color: '#fff',alignSelf: 'flex-end',marginRight: 30}}>743509-001</Text>
                 </View>
             </View>

             <View style={{flexDirection: 'row', marginLeft: 30, marginTop: 30}}>
               <Image source={require('../../app/images/ic_right.png')} style={{height: 48, width: 56}}></Image>
               <Text style={{alignSelf: 'center',fontSize: 20,color: '#000',marginLeft: 10}}>Payment Successful</Text>
             </View>
             <Text style={{marginLeft: 30,marginTop: 10,color: '#000',fontSize: 14}}>Thank you for your payment.</Text>
             <Text style={{marginLeft: 30,marginTop: 10,color: '#000',fontSize: 14,textAlign: "justify", marginRight: 30}}>This page serves as your receipt and provides you with the confirmation number ****** that can be used for further reference. </Text>

          <View style={styles.buttonContainer}>
            <TouchableHighlight
              onPress={this.login}
              style={[styles.btn]}>
              <Text style={{color: 'white'}}>MAKE ANOTHER PAYMENT</Text>
            </TouchableHighlight>
          </View>

        </View>
      );
    }
    login=()=>{
      console.log('Hi button click');
      alert("make another payment......");
    }
    backArrow=()=>{
      // alert("Go to account list......");
      this.props.navigation.goBack();
    }
  }


  const styles = StyleSheet.create({

    MainContainer :{
         backgroundColor: "#fff",
         // Setting up View inside content in Vertically center.
           justifyContent: 'center',
           flex:1,
           margin: 0
         },
         signoutContainer: {
          flex: 1,  
          justifyContent: 'flex-end',
          alignItems: 'center'
         },
         numberContainer: {
          flex: 1,  
          justifyContent: 'flex-end',
          alignItems: 'center',
          backgroundColor: '#fff000'
         },
         signOutImage:{
          alignSelf: 'flex-end',
          width: 35,
          height: 35,
         },
         buttonContainer: {
          flex: 1,
          backgroundColor: '#FFF',
         },

         btn:{
           alignSelf: 'stretch',
           borderRadius: 6,
           backgroundColor: '#2f5597',
           padding: 10,
           marginTop: 30,
           marginLeft: 30,
           marginRight: 30,
           alignItems: 'center',
         }
  });

这是上面代码的屏幕截图。

enter image description here

在这里,左箭头图像单击正常。我已经为那个灰色图像写了相同的说明。但是灰色图像单击不起作用。在这里,我尝试了两种方式,但是没有用。

onPress={() => this.backArrow()}
onPress={this.login}

因此,请指导我如何解决此问题。在此先感谢。

4 个答案:

答案 0 :(得分:0)

这可能是由于具有约束力的问题。尝试像下面那样绑定onclick方法

  constructor(props) {
    super(props);
    this.login = this.login.bind(this);    
  }

然后将该函数称为

onPress={this.login}

答案 1 :(得分:0)

尝试仅将 onPress = {this.backArrow}

答案 2 :(得分:0)

这是设计的问题,如果我更改了设计,它就可以正常工作。在这里,我更改了flex-direction:“行”

<View style={{backgroundColor: "#fff",padding: 10,*flexDirection: 'row'*}}>
                 <Image source={require('../../app/images/logo_SEDC_110n40.png')} style={{marginLeft: -10}}></Image>
                 <TouchableOpacity onPress={this.logout} style={styles.signoutContainer}> 
                    <Image source={require('../../app/images/ic_Signout.png')} style={styles.signOutImage}></Image>
                 </TouchableOpacity>
             </View>

答案 3 :(得分:0)

很抱歉,您的回复很晚, 它无法获取flex,因为父视图不包含任何flex。所以试试这个:-

run_pty

或从Touchable Opacity样式中删除flex:1。现在它将像魅力一样工作。