1 importandroid.content.Context;2 importandroid.view.View;3 importandroid.widget.ImageView;4 importandroid.widget.LinearLayout;5 importandroid.widget.Toast;6 7 /**

8 * Created by wangwentao on 2017/1/25.9 * Toast统一管理类10 */

11 12 public classToastUtil {13  private static boolean isShow = true;//默认显示

14  private static Toast mToast = null;//全局唯一的Toast

15 16 /**

17 *private控制不应该被实例化*/

18  privateToastUtil() {19   throw new UnsupportedOperationException("不能被实例化");20 }21 22  /**

23 * 全局控制是否显示Toast24 *@paramisShowToast25 */

26  public static void controlShow(booleanisShowToast){27   isShow =isShowToast;28 }29 30  /**

31 * 取消Toast显示32 */

33  public voidcancelToast() {34   if(isShow && mToast != null){35 mToast.cancel();36 }37 }38 39  /**

40 * 短时间显示Toast41 *42 *@paramcontext43 *@parammessage44 */

45  public static voidshowShort(Context context, CharSequence message) {46   if(isShow){47    if (mToast == null) {48     mToast =Toast.makeText(context, message, Toast.LENGTH_SHORT);49    } else{50 mToast.setText(message);51 }52 mToast.show();53 }54 }55 56  /**

57 * 短时间显示Toast58 *59 *@paramcontext60 *@paramresId 资源ID:getResources().getString(R.string.xxxxxx);61 */

62  public static void showShort(Context context, intresId) {63   if(isShow){64    if (mToast == null) {65     mToast =Toast.makeText(context, resId, Toast.LENGTH_SHORT);66    } else{67 mToast.setText(resId);68 }69 mToast.show();70 }71 }72 73  /**

74 * 长时间显示Toast75 *76 *@paramcontext77 *@parammessage78 */

79  public static voidshowLong(Context context, CharSequence message) {80   if(isShow){81    if (mToast == null) {82     mToast =Toast.makeText(context, message, Toast.LENGTH_LONG);83    } else{84 mToast.setText(message);85 }86 mToast.show();87 }88 }89 90  /**

91 * 长时间显示Toast92 *93 *@paramcontext94 *@paramresId 资源ID:getResources().getString(R.string.xxxxxx);95 */

96  public static void showLong(Context context, intresId) {97   if(isShow){98    if (mToast == null) {99     mToast =Toast.makeText(context, resId, Toast.LENGTH_LONG);100    } else{101 mToast.setText(resId);102 }103 mToast.show();104 }105 }106 107  /**

108 * 自定义显示Toast时间109 *110 *@paramcontext111 *@parammessage112 *@paramduration 单位:毫秒113 */

114  public static void show(Context context, CharSequence message, intduration) {115   if(isShow){116    if (mToast == null) {117     mToast =Toast.makeText(context, message, duration);118    } else{119 mToast.setText(message);120 }121 mToast.show();122 }123 }124 125  /**

126 * 自定义显示Toast时间127 *128 *@paramcontext129 *@paramresId 资源ID:getResources().getString(R.string.xxxxxx);130 *@paramduration 单位:毫秒131 */

132  public static void show(Context context, int resId, intduration) {133   if(isShow){134    if (mToast == null) {135     mToast =Toast.makeText(context, resId, duration);136    } else{137 mToast.setText(resId);138 }139 mToast.show();140 }141 }142 143  /**

144 * 自定义Toast的View145 *@paramcontext146 *@parammessage147 *@paramduration 单位:毫秒148 *@paramview 显示自己的View149 */

150  public static void customToastView(Context context, CharSequence message, intduration,View view) {151   if(isShow){152    if (mToast == null) {153     mToast =Toast.makeText(context, message, duration);154    } else{155 mToast.setText(message);156 }157    if(view != null){158 mToast.setView(view);159 }160 mToast.show();161 }162 }163 164  /**

165 * 自定义Toast的位置166 *@paramcontext167 *@parammessage168 *@paramduration 单位:毫秒169 *@paramgravity170 *@paramxOffset171 *@paramyOffset172 */

173  public static void customToastGravity(Context context, CharSequence message, int duration,int gravity, int xOffset, intyOffset) {174   if(isShow){175    if (mToast == null) {176     mToast =Toast.makeText(context, message, duration);177    } else{178 mToast.setText(message);179 }180 mToast.setGravity(gravity, xOffset, yOffset);181 mToast.show();182 }183 }184 185  /**

186 * 自定义带图片和文字的Toast,最终的效果就是上面是图片,下面是文字187 *@paramcontext188 *@parammessage189 *@paramiconResId 图片的资源id,如:R.drawable.icon190 *@paramduration191 *@paramgravity192 *@paramxOffset193 *@paramyOffset194 */

195  public static void showToastWithImageAndText(Context context, CharSequence message, int iconResId,int duration,int gravity, int xOffset, intyOffset) {196   if(isShow){197    if (mToast == null) {198     mToast =Toast.makeText(context, message, duration);199    } else{200 mToast.setText(message);201 }202 mToast.setGravity(gravity, xOffset, yOffset);203    LinearLayout toastView =(LinearLayout) mToast.getView();204    ImageView imageView = newImageView(context);205 imageView.setImageResource(iconResId);206    toastView.addView(imageView, 0);207 mToast.show();208 }209 }210 211  /**

212 * 自定义Toast,针对类型CharSequence213 *@paramcontext214 *@parammessage215 *@paramduration216 *@paramview217 *@paramisGravity true,表示后面的三个布局参数生效,false,表示不生效218 *@paramgravity219 *@paramxOffset220 *@paramyOffset221 *@paramisMargin true,表示后面的两个参数生效,false,表示不生效222 *@paramhorizontalMargin223 *@paramverticalMargin224 */

225  public static void customToastAll(Context context, CharSequence message, int duration,View view, boolean isGravity,int gravity, int xOffset, int yOffset,boolean isMargin,float horizontalMargin, floatverticalMargin) {226   if(isShow){227    if (mToast == null) {228     mToast =Toast.makeText(context, message, duration);229    } else{230 mToast.setText(message);231 }232    if(view != null){233 mToast.setView(view);234 }235    if(isMargin){236 mToast.setMargin(horizontalMargin, verticalMargin);237 }238    if(isGravity){239 mToast.setGravity(gravity, xOffset, yOffset);240 }241 mToast.show();242 }243 }244 245  /**

246 * 自定义Toast,针对类型resId247 *@paramcontext248 *@paramresId249 *@paramduration250 *@paramview :应该是一个布局,布局中包含了自己设置好的内容251 *@paramisGravity true,表示后面的三个布局参数生效,false,表示不生效252 *@paramgravity253 *@paramxOffset254 *@paramyOffset255 *@paramisMargin true,表示后面的两个参数生效,false,表示不生效256 *@paramhorizontalMargin257 *@paramverticalMargin258 */

259  public static void customToastAll(Context context, int resId, int duration,View view,boolean isGravity,int gravity, int xOffset, int yOffset,boolean isMargin,float horizontalMargin, floatverticalMargin) {260   if(isShow){261    if (mToast == null) {262     mToast =Toast.makeText(context, resId, duration);263    } else{264 mToast.setText(resId);265 }266    if(view != null){267 mToast.setView(view);268 }269    if(isMargin){270 mToast.setMargin(horizontalMargin, verticalMargin);271 }272    if(isGravity){273 mToast.setGravity(gravity, xOffset, yOffset);274 }275 mToast.show();276 }277 }278 }

android封装全局调用的toast_Android实用的Toast工具类封装相关推荐

  1. android封装全局调用的toast_Android Toast提示封装实例代码

    Android Toast提示封装 Android中经常用到Toast提示,项目中很多Toast提示,写很长的一行,简单的封装一下,将Toast方法提出来,很方便使用: 实例代码: /** * 提示字 ...

  2. ElasticSearch工具类封装

    最近在项目中有看到一种比较实用的ElasticSearch工具类封装方式,特此记录便于日后查阅.         1.controller层 @RequestMapping(value = " ...

  3. Android 图片处理工具类封装2

    http://www.2cto.com/kf/201312/263638.html Android 图片处理工具类封装 2013-12-10     0个评论   来源:Wiker Yong 的专栏  ...

  4. 【Android 插件化】Hook 插件化框架 ( 反射工具类 | 反射常用操作整理 )

    Android 插件化系列文章目录 [Android 插件化]插件化简介 ( 组件化与插件化 ) [Android 插件化]插件化原理 ( JVM 内存数据 | 类加载流程 ) [Android 插件 ...

  5. java轻量级并行工具类_16 个超级实用的 Java 工具类

    原标题:16 个超级实用的 Java 工具类 源 /juejin 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名, ...

  6. Android旋转视频工具类,Android开发实现的IntentUtil跳转多功能工具类【包含视频、音频、图片、摄像头等操作功能】...

    本文实例讲述了Android开发实现的IntentUtil跳转多功能工具类.分享给大家供大家参考,具体如下: 说明:此工具类是本人开发中总结下来的,还有其它的跳转亲给我留言,希望大家一起把这个工具类打 ...

  7. Redis工具类封装讲解和实战

    Redis工具类封装讲解和实战     简介:高效开发方式 Redis工具类封装讲解和实战         1.常用客户端 https://redisdesktop.com/download      ...

  8. IOS开发基础之音频工具类封装AVAudioPlayer

    IOS开发基础之音频工具类封装AVAudioPlayer 源码在我的主页下面 ,项目名称是AVAudioPlayer 关键性代码 工具类的封装 // // LJAudioTool.h // AVAud ...

  9. Android Log工具类,Toast工具类,获取当前方法名称

    Log新晋工具方法 public class LgqLog {private static boolean ifShow=true;private static int sCurrentLogLeve ...

最新文章

  1. 通过shell脚本定期更新elasticsearch数据
  2. 使用ssh做端口转发
  3. Angular2 - Starter - Routes, Route Resolver
  4. 其它项目中引用AirSIm模块报错NullReferenceException: Object reference not set to an instance of an object
  5. css中哪些属性与创建多列相关,css3中的新增属性有哪些
  6. 粒子群算法求解带约束优化问题 源码实现
  7. 传统生成API文档弊端
  8. hilbert变换_希尔伯特变换 matlab实现
  9. [css] 怎么设置可点击的元素上强制手型?
  10. js中 var a= b || c;
  11. 苹果今年秋季或发布史上最多新品
  12. 快速实现python c扩展模块
  13. 揭秘硅谷传奇:惠普的创业故事
  14. win10家庭版升级到企业版的教程
  15. 三毛的创作姿态与文体选择
  16. 一键解锁iPhone屏幕密码
  17. 学习金蝶ERP 之 K3 介绍
  18. 引领企业电销革新,外呼系统是不可缺的电销工具
  19. RocketMQ4.X消息队列详细笔记
  20. python蓝桥杯 既约分数

热门文章

  1. SAP Spartacus cypress集成测试执行失败的一些常见原因
  2. SAP UI5里input field live search的实现例子
  3. SAP Spartacus里product数据请求的HTTP url是在哪里维护的
  4. TypeScript的类型断言,有点像ABAP的强制类型转换
  5. 解决MySQL Server Logs不能正常查看的问题
  6. why configuration from BSP application is loaded
  7. SAP Fiori Elements - bindComponent - binding property in XML view will trigger odata request
  8. SpringBoot的配置优先级,一个具体的练习例子
  9. metadata request forward to GM6 - X-DevTools-Emulate-Network-Conditions-Cli
  10. Cloud for Customer里XML view的加载原理