Toast工具类

封装的吐司的工具类

public class ToastUtils {

	private static final String TAG = ToastUtils.class.getSimpleName();
	
	private static Toast mCustomToast;
	private static Toast mToast;
	private static TextView mText;
	
	private ToastUtils(){};
	
	public static void showCustom(Context context, String text) {
		Resources resources = context.getResources();
		int iconId = 0;
		if (text.contains("失败")) {
			iconId = resources.getIdentifier("ic_toast_error", "drawable", context.getPackageName());
		} else {
			iconId = resources.getIdentifier("ic_tick", "drawable", context.getPackageName());
		}
		showCustom(context, iconId, text);
	}
	
	public static void showCustom(Context context, int iconId, String text) {
		Resources resources = context.getResources();
		if(mCustomToast == null) {
			LayoutInflater inflater = LayoutInflater.from(context);
			int resId = resources.getIdentifier("toast_view", "layout", context.getPackageName());
			if (resId == 0) {
				Log.e(TAG, "cannot find layout toast_view!");
			}
			View view = inflater.inflate(resId, null);
			int textId = resources.getIdentifier("text", "id", context.getPackageName());
			mText = (TextView)view.findViewById(textId);
			mCustomToast = new Toast(context);
			mCustomToast.setGravity(Gravity.CENTER, 0, 0);
			mCustomToast.setView(view);
		}
		mText.setCompoundDrawablesWithIntrinsicBounds(0, iconId, 0, 0);
		mText.setText(text);
		mCustomToast.show();
	}
	
	public static void show(Context context, String text) {
		show(context, text, false);
	}
	
	public static void show(Context context, String text, boolean isLongTime) {
		if(mToast == null) {
			mToast = Toast.makeText(context, text, isLongTime == true ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
		} else {
			mToast.setText(text);
			mToast.setDuration(isLongTime == true ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
		}
		mToast.show();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值