Skip to main content

ToastAndroid

React Native 的 ToastAndroid API 将 Android 平台的 ToastAndroid 模块公开为 JS 模块。它提供了方法 show(message, duration),该方法采用以下参数:

¥React Native's ToastAndroid API exposes the Android platform's ToastAndroid module as a JS module. It provides the method show(message, duration) which takes the following parameters:

  • message 包含要吐司文本的字符串

    ¥message A string with the text to toast

  • 持续时间 Toast 的持续时间 — ToastAndroid.SHORTToastAndroid.LONG

    ¥duration The duration of the toast—either ToastAndroid.SHORT or ToastAndroid.LONG

你也可以使用 showWithGravity(message, duration, gravity) 指定 toast 在屏幕布局中的显示位置。可能是 ToastAndroid.TOPToastAndroid.BOTTOMToastAndroid.CENTER

¥You can alternatively use showWithGravity(message, duration, gravity) to specify where the toast appears in the screen's layout. May be ToastAndroid.TOP, ToastAndroid.BOTTOM or ToastAndroid.CENTER.

showWithGravityAndOffset(message, duration, gravity, xOffset, yOffset) 方法添加了以像素为单位指定偏移量的功能。

¥The showWithGravityAndOffset(message, duration, gravity, xOffset, yOffset) method adds the ability to specify an offset with in pixels.

从 Android 11(API 级别 30)开始,设置重力对文本提示没有影响。阅读有关 此处 的更改。

¥Starting with Android 11 (API level 30), setting the gravity has no effect on text toasts. Read about the changes here.


参考

¥Reference

方法

¥Methods

show()

tsx
static show(message: string, duration: number);

showWithGravity()

此属性仅适用于 Android API 29 及以下版本。对于更高 Android API 上的类似功能,请考虑使用 snackbar 或通知。

¥This property will only work on Android API 29 and below. For similar functionality on higher Android APIs, consider using snackbar or notification.

tsx
static showWithGravity(message: string, duration: number, gravity: number);

showWithGravityAndOffset()

此属性仅适用于 Android API 29 及以下版本。对于更高 Android API 上的类似功能,请考虑使用 snackbar 或通知。

¥This property will only work on Android API 29 and below. For similar functionality on higher Android APIs, consider using snackbar or notification.

tsx
static showWithGravityAndOffset(
message: string,
duration: number,
gravity: number,
xOffset: number,
yOffset: number,
);

属性

¥Properties

SHORT

指示屏幕上的持续时间。

¥Indicates the duration on the screen.

tsx
static SHORT: number;

LONG

指示屏幕上的持续时间。

¥Indicates the duration on the screen.

tsx
static LONG: number;

TOP

指示屏幕上的位置。

¥Indicates the position on the screen.

tsx
static TOP: number;

BOTTOM

指示屏幕上的位置。

¥Indicates the position on the screen.

tsx
static BOTTOM: number;

CENTER

指示屏幕上的位置。

¥Indicates the position on the screen.

tsx
static CENTER: number;