Android Toast統一管理類:
java
import android.content.Context; import android.widget.Toast; /** * Toast統一管理類 */ public class ToastUtils { private static Toast toast; /** * 短期顯示Toast */ public static void showShort(Context context, CharSequence message) { if (null == toast) { toast = Toast.makeText(context, message, Toast.LENGTH_SHORT); // toast.setGravity(Gravity.CENTER, 0, 0); } else { toast.setText(message); } toast.show(); } /** * 短期顯示Toast */ public static void showShort(Context context, int message) { if (null == toast) { toast = Toast.makeText(context, message, Toast.LENGTH_SHORT); // toast.setGravity(Gravity.CENTER, 0, 0); } else { toast.setText(message); } toast.show(); } /** * 長時間顯示Toast */ public static void showLong(Context context, CharSequence message) { if (null == toast) { toast = Toast.makeText(context, message, Toast.LENGTH_LONG); // toast.setGravity(Gravity.CENTER, 0, 0); } else { toast.setText(message); } toast.show(); } /** * 長時間顯示Toast */ public static void showLong(Context context, int message) { if (null == toast) { toast = Toast.makeText(context, message, Toast.LENGTH_LONG); // toast.setGravity(Gravity.CENTER, 0, 0); } else { toast.setText(message); } toast.show(); } /** * 自定義顯示Toast時間 */ public static void show(Context context, CharSequence message, int duration) { if (null == toast) { toast = Toast.makeText(context, message, duration); // toast.setGravity(Gravity.CENTER, 0, 0); } else { toast.setText(message); } toast.show(); } /** * 自定義顯示Toast時間 */ public static void show(Context context, int message, int duration) { if (null == toast) { toast = Toast.makeText(context, message, duration); // toast.setGravity(Gravity.CENTER, 0, 0); } else { toast.setText(message); } toast.show(); } /** * Hide the toast, if any. */ public static void hideToast() { if (null != toast) { toast.cancel(); } } }