针对两种错误,我想展示两个不同的警报。
在显示下一个警报之前,我想要移除已经存在的任何警报。
当前,一个警报会出现在上一个警报的上方,
如何在显示下一个警报之前解除一个警报?
针对两种错误,我想展示两个不同的警报。
在显示下一个警报之前,我想要移除已经存在的任何警报。
当前,一个警报会出现在上一个警报的上方,
如何在显示下一个警报之前解除一个警报?
要关闭React Native警告框,只需这样做:
Alert.alert(
'No Internet !',
'Your internet does not seems to work',
[
{ text: "Try again", onPress: () =>this.myfunction()},
{ text: "Dismiss", onPress: () =>console.log('dismissing alert')}
],
{ cancelable: false }
)
To handle this situation, the best solution is to design custom alert by using **react-native-dialog**. here is running sample code, it works fine for me and you can give your custom color and style in this alert
import React, { Component } from 'react';
import { StyleSheet, Platform, View, Text,TouchableOpacity } from 'react-native';
// import PropTypes from 'prop-types';
// import Dialog from "react-native-dialog";
import Alertfunction from './src/CustomAlert'
export default class App extends Component{
render() {
return (
<Alertfunction Title={"Alert"} FontSize = {30} FontColor= '#FF9800' Visible={true}/>
);
}
}
const styles = StyleSheet.create({
MainContainer :{
flex:1,
paddingTop: (Platform.OS) === 'ios' ? 20 : 0,
alignItems: 'center',
justifyContent: 'center',
}
});
CustomAlert.js
import React, { Component } from 'react';
import { StyleSheet, Platform, View, Text,TouchableOpacity } from 'react-native';
import PropTypes from 'prop-types';
import Dialog from "react-native-dialog";
class Alertfunction extends Component {
state = {
dialogVisible: this.props.Visible
};
showDialog = () => {
this.setState({ dialogVisible: this.props.Visible });
};
handleCancel = () => {
this.setState({ dialogVisible: false });
// this.props.Visible=false;
};
handleDelete = () => {
this.setState({ dialogVisible: false });
//this.props.Visible=false;
};
render() {
return (
<View>
<TouchableOpacity onPress={this.showDialog}>
<Text >{this.props.Title}</Text>
</TouchableOpacity>
<Dialog.Container visible={this.state.dialogVisible}>
<Dialog.Title style={{fontSize : this.props.FontSize, color: this.props.FontColor}}>{this.props.Title}</Dialog.Title>
<Dialog.Description style={{fontSize : this.props.FontSize, color: this.props.FontColor}}>
Do you want to delete this account? You cannot undo this action.
</Dialog.Description>
<Dialog.Button style={{fontSize : this.props.FontSize, color: this.props.FontColor}} label="Cancel" onPress={this.handleCancel} />
<Dialog.Button style={{fontSize : this.props.FontSize, color: this.props.FontColor}} label="ok" onPress={this.handleDelete} />
</Dialog.Container>
</View>
);
}
}
export default Alertfunction;
Alertfunction.propTypes =
{
Title: PropTypes.string,
FontSize: PropTypes.number,
FontColor: PropTypes.string,
Visible: PropTypes.bool,
}
Alertfunction.defaultProps =
{
Title: "Default Name",
FontColor: "#00E676",
FontSize: 15,
Visible:false
}
const styles = StyleSheet.create({
MainContainer :{
flex:1,
paddingTop: (Platform.OS) === 'ios' ? 20 : 0,
alignItems: 'center',
justifyContent: 'center',
}
});
您可以使用传递给Alert.alert(title, message, buttons, {cancelable: true}
的cancelable
选项。
此外,您还可以传递一个onDismiss
函数,以便您知道用户没有选择任何内容!
希望这有所帮助!
{ cancelable: false } 这个选项只在安卓系统中有效,iOS系统中无效。
你需要使用自定义的警告框来实现这个功能。
实际上你可以!
例如:
Alert.alert(
'Oops!',
'The provided passwords did not match',
[
{ text: "Try again", onPress: () => null}
],
{ cancelable: false }
)