Alert
Ta strona została przetłumaczona przez PageTurner AI (beta). Nie jest oficjalnie zatwierdzona przez projekt. Znalazłeś błąd? Zgłoś problem →
Wyświetla okno dialogowe alertu z podanym tytułem i wiadomością.
Opcjonalnie możesz podać listę przycisków. Dotknięcie dowolnego przycisku wywoła odpowiednie wywołanie zwrotne (callback) onPress i zamknie alert. Domyślnie dostępny jest tylko przycisk 'OK'.
Jest to interfejs API działający zarówno na Androidzie, jak i iOS, który wyświetla statyczne alerty. Alert z możliwością wprowadzania informacji dostępny jest tylko na iOS.
Przykład
iOS
Na iOS możesz określić dowolną liczbę przycisków. Każdy przycisk może opcjonalnie mieć określony styl lub być wyróżniony - dostępne opcje reprezentuje enum AlertButtonStyle i pole isPreferred w AlertButton.
Android
Na Androidzie można określić maksymalnie trzy przyciski. System rozróżnia przyciski neutralne, negatywne i pozytywne:
-
Jeden przycisk będzie przyciskiem 'pozytywnym' (np. 'OK')
-
Dwa przyciski oznaczają 'negatywny' i 'pozytywny' (np. 'Anuluj', 'OK')
-
Trzy przyciski oznaczają 'neutralny', 'negatywny', 'pozytywny' (np. 'Później', 'Anuluj', 'OK')
Alerty na Androidzie można zamknąć przez dotknięcie poza oknem dialogowym. Domyślnie ta funkcja jest wyłączona - możesz ją aktywować, podając opcjonalny parametr AlertOptions z właściwością cancelable ustawioną na true, czyli:{cancelable: true}.
Zdarzenie anulowania można obsłużyć, podając właściwość callbacku onDismiss w parametrze options.
Example Android
Dokumentacja
Metody
alert()
static alert (
title: string,
message?: string,
buttons?: AlertButton[],
options?: AlertOptions,
);
Parametry:
| 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,
);
Tworzy i wyświetla monit o wprowadzenie tekstu w formie alertu.
Parametry:
| 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. |
Definicje Typów
AlertButtonStyle iOS
Styl przycisku alertu na iOS.
| Type |
|---|
| enum |
Stałe:
| Value | Description |
|---|---|
'default' | Default button style. |
'cancel' | Cancel button style. |
'destructive' | Destructive button style. |
AlertType iOS
Typ alertu na iOS.
| Type |
|---|
| enum |
Stałe:
| 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
Obiekt opisujący konfigurację przycisku w alercie.
| Type |
|---|
| array of objects |
Właściwości obiektu:
| 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 |
Właściwości:
| 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. |