溫馨提示×

android中toast封裝的方法是什么

小億
114
2023-12-26 18:07:42
欄目: 編程語言

在Android中,Toast是一種用于顯示簡短消息的小彈窗。封裝Toast的常見方法是創建一個工具類,通過靜態方法來顯示Toast消息。以下是一個簡單的封裝Toast的方法示例:

public class ToastUtils {
    private static Toast toast;

    public static void showToast(Context context, String message) {
        if (toast != null) {
            toast.cancel();
        }
        toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
        toast.show();
    }
}

使用該工具類,你可以在應用的任何地方顯示Toast消息,只需調用ToastUtils.showToast(context, message)方法即可。

例如,在Activity中顯示一個Toast消息:

ToastUtils.showToast(MainActivity.this, "Hello, World!");

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女