您的位置:首页 > 移动开发 > Android开发

安卓常用工具类-ToastUtils【提示工具类】

2017-10-22 11:39 316 查看

该工具类做了优化处理,若A消息正在显示提示,此时B消息进来,会直接改变提示的文字内容。不会等A消息结束再弹出。提高了用户体验~

package com.example.burro.demo.appframework.util;

import android.content.Context;
import android.os.Handler;
import android.widget.Toast;

/**
* 提示工具类
* Created by burro on 2017/9/23.
*/
public class ToastUtils {
private static Toast mToast;
private static Handler mHandler = new Handler();
private static Runnable r = new Runnable() {
public void run() {
mToast.cancel();
}
};

/**
* 提示
*
* @param mContext
* @param text     String 内容
*/
public static void showToast(Context mContext, String text) {
if (!StringUtils.isStrEmpty(text)) {
if (mToast != null)
mToast.setText(text.trim());
else
mToast = Toast.makeText(mContext, text.trim(), Toast.LENGTH_SHORT);
mToast.show();
}
}

public static void showToast(Context mContext, String text, int duration) {
if (!StringUtils.isStrEmpty(text)) {
mHandler.removeCallbacks(r);
if (mToast != null)
mToast.setText(text);
else
mToast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
mHandler.postDelayed(r, duration);

mToast.show();
}
}

public static void showToast(Context mContext, int resId, int duration) {
showToast(mContext, mContext.getResources().getString(resId), duration);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android Toast工具类