启动一个提示对话框,包含对应的标题和信息。
你还可以指定一系列的按钮,点击对应的按钮会调用对应的 onPress 回调并且关闭提示框。默认情况下,对话框会仅有一个'确定'按钮。
本接口可以在 iOS 和 Android 上显示一个静态的提示框。如果要在显示提示框的同时接受用户输入一些信息,那你可能需要AlertIOS
。
示例
import React, { useState } from "react" ;
import { View, StyleSheet, Button, Alert } from "react-native" ;
const App = () => {
const createTwoButtonAlert = () =>
Alert.alert(
"Alert Title" ,
"My Alert Msg" ,
[
{
text : "Cancel" ,
onPress : () => console .log("Cancel Pressed" ),
style : "cancel"
},
{ text : "OK" , onPress : () => console .log("OK Pressed" ) }
]
);
const createThreeButtonAlert = () =>
Alert.alert(
"Alert Title" ,
"My Alert Msg" ,
[
{
text : "Ask me later" ,
onPress : () => console .log("Ask me later pressed" )
},
{
text : "Cancel" ,
onPress : () => console .log("Cancel Pressed" ),
style : "cancel"
},
{ text : "OK" , onPress : () => console .log("OK Pressed" ) }
]
);
return (
<View style ={styles.container} >
<Button title ={ "2-Button Alert "} onPress ={createTwoButtonAlert} />
<Button title ={ "3-Button Alert "} onPress ={createThreeButtonAlert} />
</View >
);
}
const styles = StyleSheet.create({
container : {
flex : 1 ,
justifyContent : "space-around" ,
alignItems : "center"
}
});
export default App;
import React, { Component } from "react" ;
import { View, StyleSheet, Button, Alert } from "react-native" ;
class App extends Component {
createTwoButtonAlert = () =>
Alert.alert(
"Alert Title" ,
"My Alert Msg" ,
[
{
text : "Cancel" ,
onPress : () => console .log("Cancel Pressed" ),
style : "cancel"
},
{ text : "OK" , onPress : () => console .log("OK Pressed" ) }
]
);
createThreeButtonAlert = () =>
Alert.alert(
"Alert Title" ,
"My Alert Msg" ,
[
{
text : "Ask me later" ,
onPress : () => console .log("Ask me later pressed" )
},
{
text : "Cancel" ,
onPress : () => console .log("Cancel Pressed" ),
style : "cancel"
},
{ text : "OK" , onPress : () => console .log("OK Pressed" ) }
]
);
render() {
return (
<View style ={styles.container} >
<Button title ={ "2-Button Alert "} onPress ={this.createTwoButtonAlert} />
<Button
title ={ "3-Button Alert "}
onPress ={this.createThreeButtonAlert}
/>
</View >
);
}
}
const styles = StyleSheet.create({
container : {
flex : 1 ,
justifyContent : "space-around" ,
alignItems : "center"
}
});
export default App;
iOS
在 iOS 上你可以指定任意数量的按钮。每个按钮还都可以指定自己的样式,此外还可以指定提示的类别。参阅AlertIOS 来了解更多细节。
Android
在 Android 上最多能指定三个按钮,这三个按钮分别具有“中间态”、“消极态”和“积极态”的概念:
如果你只指定一个按钮,则它具有“积极态”的属性(比如“确定”);两个按钮,则分别是“消极态”和“积极态”(比如“取消”和“确定”);三个按钮则意味着“中间态”、“消极态”和“积极态”(比如“稍候再说”,“取消”,“确定”)。
在 Android 上可以通过点击提示框的外面来取消提示框,但这一行为默认没有启用。你可以在options
中提供一个额外参数来启用这一行为:{ cancelable: true }
。
The cancel event can be handled by providing an onDismiss
callback property { onDismiss: () => {} }
inside the options
parameter.
一个简单的例子:
Alert. alert ( 'Alert Title' , 'My Alert Msg' , [
{
text: 'Ask me later' ,
onPress : ( ) => console. log ( 'Ask me later pressed' )
} ,
{
text: 'Cancel' ,
onPress : ( ) => console. log ( 'Cancel Pressed' ) ,
style: 'cancel'
} ,
{ text: 'OK' , onPress : ( ) => console. log ( 'OK Pressed' ) } ,
{
cancelable: true ,
onDismiss : ( ) =>
console. log (
'This alert was dismissed by tapping outside of the alert dialog.'
)
}
] ) ;
文档
方法
alert()
static alert ( title, message? , buttons? , options? )