zoukankan      html  css  js  c++  java
  • android之Toast工具类

    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));
        }
    }
  • 相关阅读:
    DP实验
    Linux shell学习
    线性排序算法-计数排序
    算法笔试题练习
    堆排序
    node.js初识
    linux下vim命令详解
    html5学习笔记
    有趣的参数收集
    算法学习-归并排序
  • 原文地址:https://www.cnblogs.com/kangweifeng/p/4891997.html
Copyright © 2011-2022 走看看