【发布时间】:2019-04-01 09:40:06
【问题描述】:
我正在使用此代码通过按下按钮来显示我的输入值。但是,每当我输入文本时,只要我向 组件键入任何输入,它就会自动显示一个警告框。
当我按下“添加用户”按钮时,我想显示警报框。它有什么问题?
import React, { Component } from 'react';
import {View, Text, TextInput,Alert, Button} from 'react-native';
export default class myTest extends Component{
constructor(){
super();
this.state = {firstName: 'your name', lastName: 'your last', email: 'enter your email'};
}
onPressButton(){
let firstName = this.state.firstName;
let lastName = this.state.lastName;
Alert.alert("You've Entered" + firstName+"\n "+lastName+"\n");
}
render()
{
return (
<View>
<Text>ENTER YOUR DETAILS:</Text>
<TextInput
style={{height: 40}}
placeholder="FirstName"
onChangeText={(firstName) => this.setState({firstName})}
/>
<TextInput
style={{height: 40}}
placeholder="Last Name"
onChangeText={(lastName) => this.setState({lastName})}
/>
<Text style={{padding: 10, fontSize: 30}}>
firstName: {this.state.firstName}{'\n'}
lastName: {this.state.lastName}
</Text>
<Button title="Add User" onPress={this.onPressButton()}/>
</View>
);
}
}
【问题讨论】:
标签: react-native react-native-android