Android弹出Toast工具类总结

 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);
    }
}

 

转载于:https://www.cnblogs.com/hsqdboke/p/10170360.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值