警告框
非官方测试版翻译
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
启动一个带有指定标题和消息的警告对话框。
可选提供按钮列表。点击任意按钮将触发相应的 onPress 回调并关闭警告框。默认情况下仅显示一个'确定'按钮。
这是兼容 Android 和 iOS 的 API,可用于显示静态警告框。提示用户输入信息的警告框仅在 iOS 上可用。
示例
iOS
在 iOS 上可指定任意数量的按钮。每个按钮可选择设置样式或强调效果,可用选项通过 AlertButtonStyle 枚举和 AlertButton 中的 isPreferred 字段表示。
Android
在 Android 上最多可指定三个按钮。Android 有中立按钮、否定按钮和肯定按钮的概念:
-
指定一个按钮时,它将成为'肯定'按钮(如'确定')
-
两个按钮分别表示'否定'和'肯定'(如'取消'、'确定')
-
三个按钮分别表示'中立'、'否定'和'肯定'(如'稍后'、'取消'、'确定')
在 Android 上可通过点击警告框外部区域关闭警告框。默认禁用此功能,可通过设置 AlertOptions 参数中的 cancelable 属性为 true 启用,即{cancelable: true}。
可通过在 options 参数中提供 onDismiss 回调属性处理取消事件。
Example Android
参考
方法
alert()
tsx
static alert (
title: string,
message?: string,
buttons?: AlertButton[],
options?: AlertOptions,
);
参数:
| Name | Type | Description |
|---|---|---|
| title Required | string | The dialog's title. Passing null or empty string will hide the title. |
| message | string | An optional message that appears below the dialog's title. |
| buttons | AlertButton[] | An optional array containing buttons configuration. |
| options | AlertOptions | An optional Alert configuration. |
prompt() iOS
tsx
static prompt: (
title: string,
message?: string,
callbackOrButtons?: ((text: string) => void) | AlertButton[],
type?: AlertType,
defaultValue?: string,
keyboardType?: string,
);
创建并显示提示用户输入文本的警告框。
参数:
| Name | Type | Description |
|---|---|---|
| title Required | string | The dialog's title. |
| message | string | An optional message that appears above the text input. |
| callbackOrButtons | function AlertButton[] | If passed a function, it will be called with the prompt's value(text: string) => void, when the user taps 'OK'.If passed an array, buttons will be configured based on the array content. |
| type | AlertType | This configures the text input. |
| defaultValue | string | The default text in text input. |
| keyboardType | string | The keyboard type of first text field (if exists). One of TextInput keyboardTypes. |
| options | AlertOptions | An optional Alert configuration. |
类型定义
AlertButtonStyle iOS
iOS 警告框按钮样式。
| Type |
|---|
| enum |
常量:
| Value | Description |
|---|---|
'default' | Default button style. |
'cancel' | Cancel button style. |
'destructive' | Destructive button style. |
AlertType iOS
iOS 警告框类型。
| Type |
|---|
| enum |
常量:
| Value | Description |
|---|---|
'default' | Default alert with no inputs |
'plain-text' | Plain text input alert |
'secure-text' | Secure text input alert |
'login-password' | Login and password alert |
AlertButton
描述警告框中按钮配置的对象。
| Type |
|---|
| array of objects |
对象属性:
| Name | Type | Description |
|---|---|---|
| text | string | Button label. |
| onPress | function | Callback function when button is pressed. |
| style iOS | AlertButtonStyle | Button style, on Android this property will be ignored. |
| isPreferred iOS | boolean | Whether button should be emphasized, on Android this property will be ignored. |
AlertOptions
| Type |
|---|
| object |
属性:
| Name | Type | Description |
|---|---|---|
| cancelable Android | boolean | Defines if alert can be dismissed by tapping outside of the alert box. |
| userInterfaceStyle iOS | string | The interface style used for the alert, can be set to light or dark, otherwise the default system style will be used. |
| onDismiss Android | function | Callback function fired when alert has been dismissed. |