OnPress在Touchable Opacity中无法正常工作

时间:2017-10-24 09:55:49

标签: javascript reactjs react-native touchableopacity onpress

我正在建立一个反应原生的移动应用程序!最近我为每个页面构建了自己的头部组件。这是我的Header组件的代码。我在该组件中有两个图标。但不幸的是,这些按钮根本不起作用!!

import React, {Component} from 'react';
import { 
    Text, 
    View, 
    Image, 
  Dimensions,
  TouchableOpacity
} from 'react-native';

import Icon from 'react-native-vector-icons/Ionicons';

import Styles from './Styles';

export default class ChatHead extends Component {
  constructor(props) {
      super(props);

      this.state = {
      }
  }

  render(){
    console.log(this.props.headerText, this.props);
    if(this.props.headerText.length > 16){
      name = this.props.headerText.substr(0, 16);
      name += "..."; 
    }
    else name = this.props.headerText;

    return(
      <View style={Styles.viewStyle}>
        <Text style={Styles.nameStyle}>{name}</Text>

        <TouchableOpacity 
          style={Styles.audioCallButton}
          onPress={() => console.log("Audio Button Pressed")}
        >
          <Icon name={'md-call'} size={25} color="white" align='right'/>
        </TouchableOpacity>

        <TouchableOpacity 
          style={Styles.videoCallButton}
          onPress={() => console.log("Video Button Pressed")}
        >
          <Icon name={'ios-videocam'} size={28} color="white" align='right'/>
        </TouchableOpacity>
      </View>
    );
  }
};
  

onPress根本没有回复!

3 个答案:

答案 0 :(得分:0)

情况并非如此,但对于将来的读者来说,可能是因为您将一个TouchableOpacity包裹在另一个包裹中

答案 1 :(得分:-1)

尝试此解决方案,请注意我已将您的Icon包装在视图中,并且还包含名称。

import React, {Component} from 'react';
import { 
  Text, 
  View, 
  Image, 
  Dimensions,
  TouchableOpacity
} from 'react-native';

import Icon from 'react-native-vector-icons/Ionicons';

import Styles from './Styles';

export default class ChatHead extends Component {
constructor(props) {
  super(props);

  this.state = {
  }
}

render(){
 return(
  <View style={Styles.viewStyle}>
     <Text style={Styles.nameStyle}>{this.props.headerText.length > 16 ?
`${this.props.headerText.substr(0, 16)}...` : this.props.headerText}</Text>

    <TouchableOpacity 
      style={Styles.audioCallButton}
      onPress={() => console.log("Audio Button Pressed")}
    ><View>
      <Icon name={'md-call'} size={25} color="white" align='right'/></View>
    </TouchableOpacity>

    <TouchableOpacity 
      style={Styles.videoCallButton}
      onPress={() => console.log("Video Button Pressed")}
    ><View>
      <Icon name={'ios-videocam'} size={28} color="white" align='right'/></View>
    </TouchableOpacity>
  </View>
 );
 }
};

答案 2 :(得分:-1)

尝试用alert代替console.log来查看当时和那里的数据,可能是你错过了c的位置