首页 > 移动平台 > 详细

android之Toast工具类

时间:2015-10-19 17:11:49      阅读:358      评论:0      收藏:0      [点我收藏+]
import android.content.Context;
import android.widget.Toast;

/**
 * Toast统一工具类
 * Created by Administrator on 2015/10/19 0019.
 */
public class ToastUtils {

    protected static Toast toast   = null;
    private static String oldMsg;
    private static long oneTime = 0;
    private static long twoTime = 0;

    private ToastUtils() { throw new UnsupportedOperationException("cannot be instantiated"); }

    /**
     * @param context context
     * @param msg 提示信息
     */
    public static void showToast(Context context, String msg){
        if(toast==null){
            toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
            toast.show();
            oneTime=System.currentTimeMillis();
        }else{
            twoTime=System.currentTimeMillis();
            if(msg.equals(oldMsg)){
                if(twoTime-oneTime>Toast.LENGTH_SHORT){
                    toast.show();
                }
            }else{
                oldMsg = msg;
                toast.setText(msg);
                toast.show();
            }
        }
        oneTime=twoTime;
    }

    /**
     * @param context context
     * @param resId 提示信息的资源id
     */
    public static void showToast(Context context, int resId){
        showToast(context, context.getString(resId));
    }
}

android之Toast工具类

原文:http://www.cnblogs.com/kangweifeng/p/4891997.html

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