Android 封装一个Toast工具类

简单的Toast工具类:


public class ToastUtils {

        public static void show(Context context, int resId) {
            show(context, context.getResources().getText(resId), Toast.LENGTH_SHORT);
        }

        public static void show(Context context, int resId, int duration) {
            show(context, context.getResources().getText(resId), duration);
        }

        public static void show(Context context, CharSequence text) {
            show(context, text, Toast.LENGTH_SHORT);
        }

        public static void show(Context context, CharSequence text, int duration) {
            Toast.makeText(context, text, duration).show();
        }

        public static void show(Context context, int resId, Object... args) {
            show(context, String.format(context.getResources().getString(resId), args), Toast.LENGTH_SHORT);
        }

        public static void show(Context context, String format, Object... args) {
            show(context, String.format(format, args), Toast.LENGTH_SHORT);
        }

        public static void show(Context context, int resId, int duration, Object... args) {
            show(context, String.format(context.getResources().getString(resId), args), duration);
        }

        public static void show(Context context, String format, int duration, Object... args) {
            show(context, String.format(format, args), duration);
        }

}

调用的时候ToastUtils.show();根据具体请况传入参数就OK.

猜你喜欢

转载自blog.csdn.net/yangtan_tanbaobao/article/details/82493716