React native 当我在react native中单击TouchableOpacity时,如何获取状态为的文本值? 当我在react native中单击TouchableOpacity时,如何获取状态为的文本值?

React native 当我在react native中单击TouchableOpacity时,如何获取状态为的文本值? 当我在react native中单击TouchableOpacity时,如何获取状态为的文本值?,react-native,react-native-android,react-native-ios,React Native,React Native Android,React Native Ios,我在TouchableOpacity的文本中有性别值。所以当我点击TouchableOpacity时,我想得到状态中所选性别的值 所以,请帮助我如何实现这一功能。感谢您的帮助 这是我屏幕的一部分,你可以在下面看到 示例代码库: import React, { Component } from "react"; import { View, Text, StyleSheet, TextInput, TouchableOpacity } from "react-native";

我在TouchableOpacity的文本中有性别值。所以当我点击TouchableOpacity时,我想得到状态中所选性别的值

所以,请帮助我如何实现这一功能。感谢您的帮助

这是我屏幕的一部分,你可以在下面看到

示例代码库:

import React, { Component } from "react";
import {
  View,
  Text,
  StyleSheet,
  TextInput,
  TouchableOpacity
} from "react-native";

export default class Home extends Component {
  constructor(props) {
    super(props);
    this.state = {
      gender: "",
      textGender:"Male"
    };
  }

  //Get text value
  getTextValue = () => {
    alert(this.state.gender);
    console.log("Selected Gender is ", this.state.gender);
    console.log("Selected Gender From Text ", this.state.textGender);
  };

  render() {
    return (
      <View style={styles.container}>
        <TextInput
          value={this.state.gender}
          keyboardType="default"
          onChangeText={gender => this.setState({ gender })}
        />

        <Text>{this.state.textGender}</Text>

        <TouchableOpacity
          onPress={() => this.getTextValue()}
        ></TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1
  }
});

希望这对你有帮助

onPress = (Male) => {
       this.setState({ gender: 'Male'})
    }
    onPress1 = (Female) => {
       this.setState({ gender: 'Female'})
    }

         <TouchableOpacity
                 style={styles.button}
                 onPress={this.onPress(Male)}
               >
                 <Text> Male </Text>
               </TouchableOpacity>

             <TouchableOpacity
                 style={styles.button}
                 onPress={this.onPress1(Female)}
               >
                 <Text> Female </Text>
               </TouchableOpacity>*

创建这样的函数

setGender=(props)={
  this.setState({gender:props})
}
按如下方式从按钮ON调用此函数

 onPress={()=>this.setGender("Male")}
 onPress={()=>this.setGender("Female")

并在文本中显示状态值

希望这对您有所帮助

import React, { Component } from 'react';
import { View, Text, StyleSheet, Button } from 'react-native';

export default class Home extends Component {
  state = {
    value: '',
  };

  onChangeValue = value => {
    if (value !== this.state.value) {
      this.setState({
        value: value,
      });
    }
  };

  render() {
    return (
      <View style={styles.container}>
        {this.state.value.length > 0 ? (
          <Text
            style={{
              fontSize: 40,
              alignSelf: 'center',
              justifyContent: 'center',
            }}>
            {this.state.value}
          </Text>
        ) : (
          <Text
            style={{
              fontSize: 40,
              alignSelf: 'center',
              justifyContent: 'center',
            }}>
            I identify as you
          </Text>
        )}
        <Button
          color="#000"
          title="Male"
          onPress={() => this.onChangeValue('male')}
        />
        <Button title="Female" onPress={() => this.onChangeValue('female')} />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignContent: 'center',
    justifyContent: 'center',
  },
});


请随意提问。

不透明度的文本是否可以从变量中读取?不可以。它只是写入文本组件@Gauravroyten这里有什么问题?如果您没有任何变量,那么您可以在OnPress中绘制该值。对于select Seven@Akila Devinda,我仅使用TouchableOpacity