Android实用的Toast工具类封装

 Toast这个提示框大家都晓得,显示一段时间后自动消失,不能获得焦点。但是在使用中有些问题:

1)需要弹出一个新的Toast时,上一个Toast还没有显示完
2)可能重复弹出相同的信息
3)Toast具体有哪些用法不是很熟悉,用到时导出去找
4)app退出去了,Toast还在弹
等等一系列问题
下面封装了一个工具类,帮助大家管理Toast,基本上可以满足常用的需求,如果还满足不了,那就自定义了

  1 import android.content.Context;
  2 import android.view.View;
  3 import android.widget.ImageView;
  4 import android.widget.LinearLayout;
  5 import android.widget.Toast;
  6   
  7 /**
  8  * Created by wangwentao on 2017/1/25.
  9  * Toast统一管理类
 10  */
 11   
 12 public class ToastUtil {
 13  private static boolean isShow = true;//默认显示
 14  private static Toast mToast = null;//全局唯一的Toast
 15   
 16 /**
 17  *private控制不应该被实例化*/
 18  private ToastUtil() {
 19   throw new UnsupportedOperationException("不能被实例化");
 20  }
 21   
 22  /**
 23   * 全局控制是否显示Toast
 24   * @param isShowToast
 25   */
 26  public static void controlShow(boolean isShowToast){
 27   isShow = isShowToast;
 28  }
 29   
 30  /**
 31   * 取消Toast显示
 32   */
 33  public void cancelToast() {
 34   if(isShow && mToast != null){
 35    mToast.cancel();
 36   }
 37  }
 38   
 39  /**
 40   * 短时间显示Toast
 41   *
 42   * @param context
 43   * @param message
 44   */
 45  public static void showShort(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   * 短时间显示Toast
 58   *
 59   * @param context
 60   * @param resId 资源ID:getResources().getString(R.string.xxxxxx);
 61   */
 62  public static void showShort(Context context, int resId) {
 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   * 长时间显示Toast
 75   *
 76   * @param context
 77   * @param message
 78   */
 79  public static void showLong(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   * 长时间显示Toast
 92   *
 93   * @param context
 94   * @param resId 资源ID:getResources().getString(R.string.xxxxxx);
 95   */
 96  public static void showLong(Context context, int resId) {
 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   * @param context
111   * @param message
112   * @param duration 单位:毫秒
113   */
114  public static void show(Context context, CharSequence message, int duration) {
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   * @param context
129   * @param resId 资源ID:getResources().getString(R.string.xxxxxx);
130   * @param duration 单位:毫秒
131   */
132  public static void show(Context context, int resId, int duration) {
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的View
145   * @param context
146   * @param message
147   * @param duration 单位:毫秒
148   * @param view 显示自己的View
149   */
150  public static void customToastView(Context context, CharSequence message, int duration,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   * @param context
167   * @param message
168   * @param duration 单位:毫秒
169   * @param gravity
170   * @param xOffset
171   * @param yOffset
172   */
173  public static void customToastGravity(Context context, CharSequence message, int duration,int gravity, int xOffset, int yOffset) {
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   * @param context
188   * @param message
189   * @param iconResId 图片的资源id,如:R.drawable.icon
190   * @param duration
191   * @param gravity
192   * @param xOffset
193   * @param yOffset
194   */
195  public static void showToastWithImageAndText(Context context, CharSequence message, int iconResId,int duration,int gravity, int xOffset, int yOffset) {
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 = new ImageView(context);
205    imageView.setImageResource(iconResId);
206    toastView.addView(imageView, 0);
207    mToast.show();
208   }
209  }
210   
211  /**
212   * 自定义Toast,针对类型CharSequence
213   * @param context
214   * @param message
215   * @param duration
216   * @param view
217   * @param isGravity true,表示后面的三个布局参数生效,false,表示不生效
218   * @param gravity
219   * @param xOffset
220   * @param yOffset
221   * @param isMargin true,表示后面的两个参数生效,false,表示不生效
222   * @param horizontalMargin
223   * @param verticalMargin
224   */
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, float verticalMargin) {
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,针对类型resId
247   * @param context
248   * @param resId
249   * @param duration
250   * @param view :应该是一个布局,布局中包含了自己设置好的内容
251   * @param isGravity true,表示后面的三个布局参数生效,false,表示不生效
252   * @param gravity
253   * @param xOffset
254   * @param yOffset
255   * @param isMargin true,表示后面的两个参数生效,false,表示不生效
256   * @param horizontalMargin
257   * @param verticalMargin
258   */
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, float verticalMargin) {
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 }

1)Toast底层使用handler机制,分别post一个nShow和一个mHide来控制Toast显示还是隐藏

2)Toast的视图是通过WindowManager的addView来加载的

3)先通过makeText()实例化出一个Toast,然后调用toast.Show()方法,这时并不会马上显示Toast,而是会实例化一个TN变量,然后通过service.enqueueToast()将其加到服务队列里面去等待显示。在TN中进行调控Toast的显示格式以及里面的hide()、show()方法来控制Toast的出现以及消失,强调一下的是这个队列是系统维护的,我们并不能干涉。

4)我们常传的时间参数LONG_DELAY和SHORT_DELAY具体时间分别是:3.5秒和2秒

5)如果我们自己也想实现类似Toast效果怎么办?

可以使用windowManager添加view的方式实现自己想要的效果

原文地址:https://www.cnblogs.com/Reverse-xiaoyu/p/11468905.html