This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 55).
AlertDialog
一个用于显示原生警告对话框的 Jetpack Compose AlertDialog 组件。
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
Expo UI AlertDialog 与官方 Jetpack Compose AlertDialog API 保持一致。内容通过槽位子组件(AlertDialog.Title、AlertDialog.Text、AlertDialog.ConfirmButton、AlertDialog.DismissButton、AlertDialog.Icon)提供,它们会直接映射到 Compose 的槽位参数。
安装
- npx expo install @expo/uiIf you are installing this in an existing React Native app, make sure to install expo in your project.
用法
基本警告对话框
import { useState } from 'react'; import { Host, AlertDialog, Button, TextButton, Text } from '@expo/ui/jetpack-compose'; export default function BasicAlertDialogExample() { const [visible, setVisible] = useState(false); return ( <Host matchContents> <Button onClick={() => setVisible(true)}> <Text>显示警告</Text> </Button> {visible && ( <AlertDialog onDismissRequest={() => setVisible(false)}> <AlertDialog.Title> <Text>确认操作</Text> </AlertDialog.Title> <AlertDialog.Text> <Text>你确定要继续吗?</Text> </AlertDialog.Text> <AlertDialog.ConfirmButton> <TextButton onClick={() => setVisible(false)}> <Text>确认</Text> </TextButton> </AlertDialog.ConfirmButton> <AlertDialog.DismissButton> <TextButton onClick={() => setVisible(false)}> <Text>取消</Text> </TextButton> </AlertDialog.DismissButton> </AlertDialog> )} </Host> ); }
自定义颜色
import { useState } from 'react'; import { Host, AlertDialog, Button, TextButton, Text } from '@expo/ui/jetpack-compose'; export default function CustomColorsExample() { const [visible, setVisible] = useState(false); return ( <Host matchContents> <Button onClick={() => setVisible(true)}> <Text>显示警告</Text> </Button> {visible && ( <AlertDialog onDismissRequest={() => setVisible(false)} colors={{ containerColor: '#1E1E2E', titleContentColor: '#CDD6F4', textContentColor: '#BAC2DE', }}> <AlertDialog.Title> <Text>自定义对话框</Text> </AlertDialog.Title> <AlertDialog.Text> <Text>此对话框使用自定义颜色。</Text> </AlertDialog.Text> <AlertDialog.ConfirmButton> <TextButton onClick={() => setVisible(false)}> <Text>确定</Text> </TextButton> </AlertDialog.ConfirmButton> <AlertDialog.DismissButton> <TextButton onClick={() => setVisible(false)}> <Text>取消</Text> </TextButton> </AlertDialog.DismissButton> </AlertDialog> )} </Host> ); }
带图标
import { useState } from 'react'; import { Host, AlertDialog, Button, TextButton, Text, Icon } from '@expo/ui/jetpack-compose'; export default function IconDialogExample() { const [visible, setVisible] = useState(false); return ( <Host matchContents> <Button onClick={() => setVisible(true)}> <Text>显示警告</Text> </Button> {visible && ( <AlertDialog onDismissRequest={() => setVisible(false)}> <AlertDialog.Icon> {/* 替换为你自己的图标资源 */} <Icon source={require('./info-icon.xml')} /> </AlertDialog.Icon> <AlertDialog.Title> <Text>带图标的对话框</Text> </AlertDialog.Title> <AlertDialog.Text> <Text>此对话框在标题上方有一个图标。</Text> </AlertDialog.Text> <AlertDialog.ConfirmButton> <TextButton onClick={() => setVisible(false)}> <Text>确定</Text> </TextButton> </AlertDialog.ConfirmButton> </AlertDialog> )} </Host> ); }
API
import { AlertDialog } from '@expo/ui/jetpack-compose';
Component
Type: React.Element<AlertDialogProps>
Renders an AlertDialog component with slot-based content matching the Compose API.
Content is provided via slot sub-components: AlertDialog.Title, AlertDialog.Text,
AlertDialog.ConfirmButton, AlertDialog.DismissButton, and AlertDialog.Icon.
React.ReactNodeChildren containing slot sub-components (AlertDialog.Title, AlertDialog.Text,
AlertDialog.ConfirmButton, AlertDialog.DismissButton, AlertDialog.Icon).
() => voidCallback that is called when the user tries to dismiss the dialog (for example, by tapping outside of it or pressing the back button).
Types
Colors for the alert dialog, matching AlertDialogDefaults in Compose.
| Property | Type | Description |
|---|---|---|
| containerColor(optional) | ColorValue | The background color of the dialog. |
| iconContentColor(optional) | ColorValue | The color of the icon. |
| textContentColor(optional) | ColorValue | The color of the body text. |
| titleContentColor(optional) | ColorValue | The color of the title text. |
Properties for the dialog window, matching DialogProperties in Compose.
| Property | Type | Description |
|---|---|---|
| decorFitsSystemWindows(optional) | boolean | Whether the dialog's decor fits system windows (status bar, navigation bar, and more).
When Default: true |
| dismissOnBackPress(optional) | boolean | Whether the dialog can be dismissed by pressing the back button. Default: true |
| dismissOnClickOutside(optional) | boolean | Whether the dialog can be dismissed by clicking outside of it. Default: true |
| usePlatformDefaultWidth(optional) | boolean | Whether the dialog should use the platform default width. Default: true |