首页 > 移动平台 > 详细

Android弹出Toast工具类总结

时间:2018-12-24 19:33:11      阅读:144      评论:0      收藏:0      [点我收藏+]

 Android弹出Toast工具类总结,包括系统自带的,也包括自定义的。

 

public class ToastUtil {
    public ToastUtil() {
    }

    public static Toast showShortToast(Context context, String text) {
        Toast toast = Toast.makeText(context, text, 0);
        toast.show();
        return toast;
    }

    public static Toast showShortToastCenter(Context context, String text) {
        Toast toast = Toast.makeText(context, text, 0);
        toast.setGravity(17, 0, 0);
        toast.show();
        return toast;
    }

    public static Toast showShortToast(Context context, @StringRes int textResId) {
        Toast toast = Toast.makeText(context, I18nUtil.getString(textResId, new Object[0]), 0);
        toast.show();
        return toast;
    }

    public static Toast showLongToast(Context context, String text) {
        Toast toast = Toast.makeText(context, text, 1);
        toast.show();
        return toast;
    }

    public static Toast showLongToast(Context context, @StringRes int textResId) {
        Toast toast = Toast.makeText(context, I18nUtil.getString(textResId, new Object[0]), 1);
        toast.show();
        return toast;
    }

    public static Toast showLongToastImage(Context context, @DrawableRes int imgResId) {
        Toast toast = new Toast(context);
        FrameLayout fl = new FrameLayout(context);
        ImageView iv = new ImageView(context);
        iv.setImageResource(imgResId);
        fl.addView(iv);
        toast.setView(fl);
        toast.setDuration(1);
        toast.show();
        return toast;
    }

    public static Toast showToastWithIcon(Context context, String text, @DrawableRes int img, OnAttachStateChangeListener listener, int duration) {
        Toast toast = new Toast(context);
        View container = View.inflate(context, layout.view_custom_toast_action_success, (ViewGroup)null);
        if(listener != null) {
            container.addOnAttachStateChangeListener(listener);
        }

        TextView tv = (TextView)container.findViewById(id.view_toast_text_img_tv);
        ImageView iv = (ImageView)container.findViewById(id.view_toast_text_img_iv);
        toast.setGravity(119, 0, 0);
        toast.setDuration(duration);
        toast.setView(container);
        tv.setText(text);
        iv.setImageResource(img);
        toast.show();
        return toast;
    }

    public static Toast showToastWithIcon(Context context, @StringRes int text, @DrawableRes int img, OnAttachStateChangeListener listener, int duration) {
        return showToastWithIcon(context, I18nUtil.getString(text, new Object[0]), img, listener, duration);
    }
}

 

Android弹出Toast工具类总结

原文:https://www.cnblogs.com/hsqdboke/p/10170360.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!