Przejdź do treści głównej

Alert

Nieoficjalne Tłumaczenie Beta

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()

tsx
static alert (
title: string,
message?: string,
buttons?: AlertButton[],
options?: AlertOptions,
);

Parametry:

NameTypeDescription
title
Required
stringThe dialog's title. Passing null or empty string will hide the title.
messagestringAn optional message that appears below the dialog's title.
buttonsAlertButton[]An optional array containing buttons configuration.
optionsAlertOptionsAn optional Alert configuration.

prompt()
iOS

tsx
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:

NameTypeDescription
title
Required
stringThe dialog's title.
messagestringAn optional message that appears above the text input.
callbackOrButtonsfunction
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.
typeAlertTypeThis configures the text input.
defaultValuestringThe default text in text input.
keyboardTypestringThe keyboard type of first text field (if exists). One of TextInput keyboardTypes.
optionsAlertOptionsAn optional Alert configuration.

Definicje Typów

AlertButtonStyle
iOS

Styl przycisku alertu na iOS.

Type
enum

Stałe:

ValueDescription
'default'Default button style.
'cancel'Cancel button style.
'destructive'Destructive button style.

AlertType
iOS

Typ alertu na iOS.

Type
enum

Stałe:

ValueDescription
'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:

NameTypeDescription
textstringButton label.
onPressfunctionCallback function when button is pressed.
style
iOS
AlertButtonStyleButton style, on Android this property will be ignored.
isPreferred
iOS
booleanWhether button should be emphasized, on Android this property will be ignored.

AlertOptions

Type
object

Właściwości:

NameTypeDescription
cancelable
Android
booleanDefines if alert can be dismissed by tapping outside of the alert box.
userInterfaceStyle
iOS
stringThe interface style used for the alert, can be set to light or dark, otherwise the default system style will be used.
onDismiss
Android
functionCallback function fired when alert has been dismissed.