Alerta
Esta página fue traducida por PageTurner AI (beta). No está respaldada oficialmente por el proyecto. ¿Encontraste un error? Reportar problema →
Muestra un diálogo de alerta con el título y mensaje especificados.
Opcionalmente, se puede proporcionar una lista de botones. Al pulsar cualquier botón se activará la devolución de llamada onPress correspondiente y se cerrará la alerta. Por defecto, el único botón será un botón 'Aceptar'.
Esta API funciona tanto en Android como en iOS y puede mostrar alertas estáticas. Las alertas que solicitan información al usuario solo están disponibles en iOS.
Ejemplo
iOS
En iOS puedes especificar cualquier número de botones. Cada botón puede opcionalmente definir un estilo o estar enfatizado. Las opciones disponibles están representadas por la enumeración AlertButtonStyle y el campo isPreferred en AlertButton.
Android
En Android se pueden especificar como máximo tres botones. Android tiene el concepto de botón neutral, negativo y positivo:
-
Si especificas un botón, será el 'positivo' (como 'Aceptar')
-
Dos botones significan 'negativo', 'positivo' (como 'Cancelar', 'Aceptar')
-
Tres botones significan 'neutral', 'negativo', 'positivo' (como 'Más tarde', 'Cancelar', 'Aceptar')
Las alertas en Android se pueden cerrar tocando fuera del cuadro de alerta. Esta opción está deshabilitada por defecto y se puede activar proporcionando el parámetro opcional AlertOptions con la propiedad cancelable establecida en true, es decir:{cancelable: true}.
El evento de cancelación se puede manejar proporcionando una propiedad de devolución de llamada onDismiss dentro del parámetro options.
Example Android
Referencia
Métodos
alert()
static alert (
title: string,
message?: string,
buttons?: AlertButton[],
options?: AlertOptions,
);
Parámetros:
| 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
static prompt: (
title: string,
message?: string,
callbackOrButtons?: ((text: string) => void) | AlertButton[],
type?: AlertType,
defaultValue?: string,
keyboardType?: string,
);
Crea y muestra un aviso para introducir texto en forma de Alerta.
Parámetros:
| 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. |
Definiciones de tipos
AlertButtonStyle iOS
Un estilo de botón de alerta para iOS.
| Type |
|---|
| enum |
Constantes:
| Value | Description |
|---|---|
'default' | Default button style. |
'cancel' | Cancel button style. |
'destructive' | Destructive button style. |
AlertType iOS
Un tipo de alerta para iOS.
| Type |
|---|
| enum |
Constantes:
| 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
Un objeto que describe la configuración de un botón en la alerta.
| Type |
|---|
| array of objects |
Propiedades del objeto:
| 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 |
Propiedades:
| 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. |