/** * Copyright 2016 JustWayward Team * <p> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.justwayward.reader.utils; import android.content.Context; import android.widget.Toast; /** * Toast工具类,解决多个Toast同时出现的问题 * * @author yuyh. * @date 16/4/9. */ public class ToastUtils { private static Toast mToast; private static Context context = AppUtils.getAppContext(); /********************** 非连续弹出的Toast ***********************/ public static void showSingleToast(int resId) { //R.string.** getSingleToast(resId, Toast.LENGTH_SHORT).show(); } public static void showSingleToast(String text) { getSingleToast(text, Toast.LENGTH_SHORT).show(); } public static void showSingleLongToast(int resId) { getSingleToast(resId, Toast.LENGTH_LONG).show(); } public static void showSingleLongToast(String text) { getSingleToast(text, Toast.LENGTH_LONG).show(); } /*********************** 连续弹出的Toast ************************/ public static void showToast(int resId) { getToast(resId, Toast.LENGTH_SHORT).show(); } public static void showToast(String text) { getToast(text, Toast.LENGTH_SHORT).show(); } public static void showLongToast(int resId) { getToast(resId, Toast.LENGTH_LONG).show(); } public static void showLongToast(String text) { getToast(text, Toast.LENGTH_LONG).show(); } public static Toast getSingleToast(int resId, int duration) { // 连续调用不会连续弹出,只是替换文本 return getSingleToast(context.getResources().getText(resId).toString(), duration); } public static Toast getSingleToast(String text, int duration) { if (mToast == null) { mToast = Toast.makeText(context, text, duration); } else { mToast.setText(text); } return mToast; } public static Toast getToast(int resId, int duration) { // 连续调用会连续弹出 return getToast(context.getResources().getText(resId).toString(), duration); } public static Toast getToast(String text, int duration) { return Toast.makeText(context, text, duration); } }