Android ToastUtils,吐司工具类
16lz
2021-01-23
Android ToastUtils,工具类。
import android.content.Context;import android.view.View;import android.widget.Toast;public class ToastUtils{ private static String oldMsg; protected static Toast toast = null; private static long oneTime=0; private static long twoTime=0; //显示时间长 public static void showLong(Context context, String text){ show(context,text,Toast.LENGTH_LONG); } //显示时间短 public static void showShort(Context context, String text){ show(context,text,Toast.LENGTH_SHORT); } private static void show(Context mContext,String msg,int mode){ if(toast==null){ //这样的话,不管传递什么content进来,都只会引用全局唯一的Content,不会产生内存泄露 toast =Toast.makeText(mContext.getApplicationContext(), msg, Toast.LENGTH_SHORT); toast.show(); oneTime=System.currentTimeMillis(); }else{ twoTime=System.currentTimeMillis(); if(msg.equals(oldMsg)){ if(twoTime-oneTime>mode){ toast.show(); } }else{ oldMsg = msg; toast.setText(msg); toast.show(); } } oneTime=twoTime; } }
更多相关文章
- android中的全局变量定义与使用
- Android定义的路径全局变量
- android全局Toast
- Android获得全局进程信息以及进程使用的内存情况
- Android用Application设置全局变量以及使用
- Android 扫码盒子全局接收付款码(全局事件&上层接收&多重下发)
- Android-AlarmManager(全局定时器/闹钟)
- Android 全局Activity动画设置
- android 上 webkit js 扩展之全局本地对象实现步骤