你的Toast也可以定制

项目小工具之Toast的定制

Android项目中会经常用到Toast提醒,虽然很好用但也挺麻烦。比如一个项目比较大,或者一个类/activity代码比较多,有的时候用完了不想在提醒的时候发现根本不好找了,尤其是在debug版本时可能会有很多的toast,但是正式版不需要了也不好解决这个问题,那么问题来了,就没有一个好用Toast的工具类嘛?答案当然是有的嘛,代码就在下方,仅供提供参考,不足之处欢迎留言交流。谢谢!


import android.app.Activity;
import android.widget.Toast;

import com.mengpeng.MyApplication;

/**
 * 创建: MengPeng
 * 日期: 2017/3/2 , 下午10:30.
 * 作用: 吐司弹窗工具类
 */
public class ToastUtils {

    private static Toast toast;
    private static boolean isDebugModule = true;

    /**
     * 根据所填的文本进行吐司提醒
     *
     * @param toastContent 吐司内容
     */
    public static void showToast(String toastContent) {
        if (toast == null && isDebugModule) {
            toast.makeText(MyGetApplication.getContext(), toastContent, Toast.LENGTH_SHORT);
        } else {
            toast.setText(toastContent);
        }
        toast.show();
    }

    /**
     * 根据所填的内容ID进行吐司提醒
     *
     * @param toastContentID 吐司内容ID
     * @param isAlwaysShow   正式版本是否吐司提醒
     */
    public static void showToast(int toastContentID, boolean isAlwaysShow) {
        if (isAlwaysShow) {
            String toastContent = MyGetApplication.getContext().getResources().getString(toastContentID);
            if (toast == null) {
                toast.makeText(MyGetApplication.getContext(), toastContent, Toast.LENGTH_SHORT);
            } else {
                toast.setText(toastContent);
            }
            toast.show();
        }
    }

    /**
     * 根据所填的文本进行吐司提醒
     *
     * @param toastContent 吐司内容
     * @param isAlwaysShow 正式版本是否吐司提醒
     */
    public static void showToast(String toastContent, boolean isAlwaysShow) {
        if (isAlwaysShow) {
            if (toast == null) {
                toast.makeText(MyGetApplication.getContext(), toastContent, Toast.LENGTH_SHORT);
            } else {
                toast.setText(toastContent);
            }
            toast.show();
        }

    }

    /**
     * 根据所填的内容ID进行吐司提醒
     *
     * @param toastContentID 吐司内容ID
     */
    public static void showToast(int toastContentID) {
        String toastContent = MyGetApplication.getContext().getResources().getString(toastContentID);
        if (toast == null && isDebugModule) {
            toast.makeText(MyGetApplication.getContext(), toastContent, Toast.LENGTH_SHORT);
        } else {
            toast.setText(toastContent);
        }
        toast.show();
    }

    /**
     * 在UI线程显示吐司消息
     *
     * @param toastContent 消息内容
     * @param activity     目标Activity
     */
    public static void showToastOnUIThread(final String toastContent, Activity activity) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (toast == null && isDebugModule) {
                    toast.makeText(MyGetApplication.getContext(), toastContent, Toast.LENGTH_SHORT);
                } else {
                    toast.setText(toastContent);
                }
                toast.show();
            }
        });
    }

    /**
     * 在UI线程中显示吐司消息
     *
     * @param toastContentID 消息ID
     * @param activity       目标Activity
     */
    public static void showToastOnUIThread(final int toastContentID, Activity activity) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String toastContent = MyGetApplication.getContext().getResources().getString(toastContentID);
                if (toast == null && isDebugModule) {
                    toast.makeText(MyGetApplication.getContext(), toastContent, Toast.LENGTH_SHORT);
                } else {
                    toast.setText(toastContent);
                }
                toast.show();
            }
        });
    }

    /**
     * 在UI线程中显示吐司消息
     *
     * @param toastContent 吐司内容
     * @param isAlwaysShow 签名打包的正式版中是否显示消息
     */
    public static void showToastOnUIThread(final String toastContent, final boolean isAlwaysShow, Activity activity) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (toast == null && isAlwaysShow) {
                    toast.makeText(MyGetApplication.getContext(), toastContent, Toast.LENGTH_SHORT);
                } else {
                    toast.setText(toastContent);
                }
                toast.show();
            }
        });
    }

    /**
     * 在UI线程中显示吐司消息
     *
     * @param toastContentID 吐司内容ID
     * @param isAlwaysShow   签名打包的正式版中是否显示消息
     */
    public static void showToastOnUIThread(final int toastContentID, final boolean isAlwaysShow, Activity activity) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String toastContent = MyGetApplication.getContext().getResources().getString(toastContentID);
                if (toast == null && isAlwaysShow) {
                    toast.makeText(MyGetApplication.getContext(), toastContent, Toast.LENGTH_SHORT);
                } else {
                    toast.setText(toastContent);
                }
                toast.show();
            }
        });
    }

}

Application中只需要写个getApplication方法即可,目的当然是为了获取Context啦。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值