Toast 工具类

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.laitoon.app.R;
import com.laitoon.app.app.BaseApplication;

/**
 * Toast 工具类
 * 解决Toast连续出现要等前一个时间到
 * 再显示下一个的延迟现象
 *
 * @author DB
 */
public class ToastUtil {


    private static Toast mToast;
    private static Toast mImgToast;

    /**
     * @param message
     * @param duration
     * @return
     */
    private static Toast initToast(CharSequence message, int duration) {
        if (mToast == null) {
            mToast = Toast.makeText(BaseApplication.getAppContext(), message, duration);
        } else {
            mToast.setText(message);
            mToast.setDuration(duration);
        }
        return mToast;
    }

    /**
     * 短时间显示Toast
     *
     * @param message
     */
    public static void showShort(CharSequence message) {
        //initToast(message, Toast.LENGTH_SHORT).show();
        initToast2(message);
    }

    private static Toast initToast2(CharSequence message) {
        if (mImgToast == null) {
            mImgToast = new Toast(BaseApplication.getAppContext());
        }
        View view = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout
                .new_toast_custom, null);
        TextView tv = view.findViewById(R.id.tv_toast);
        tv.setText(TextUtils.isEmpty(message) ? "" : message);
        mImgToast.setView(view);
        mImgToast.setGravity(Gravity.CENTER, 0, 0);
        mImgToast.setDuration(Toast.LENGTH_LONG);
        mImgToast.show();
        return mImgToast;
    }


    /**
     * 短时间显示Toast
     *
     * @param strResId
     */
    public static void showShort(int strResId) {
        initToast(BaseApplication.getAppContext().getResources().getText(strResId), Toast
                .LENGTH_SHORT).show();
    }

    /**
     * 长时间显示Toast
     *
     * @param message
     */
    public static void showLong(CharSequence message) {
        initToast(message, Toast.LENGTH_LONG).show();
    }

    /**
     * 长时间显示Toast
     *
     * @param strResId
     */
    public static void showLong(int strResId) {
        initToast(BaseApplication.getAppContext().getResources().getText(strResId), Toast
                .LENGTH_LONG).show();
    }

    /**
     * 自定义显示Toast时间
     *
     * @param message
     * @param duration
     */
    public static void show(CharSequence message, int duration) {
        initToast(message, duration).show();
    }

    /**
     * 自定义显示Toast时间
     *
     * @param context
     * @param strResId
     * @param duration
     */
    public static void show(Context context, int strResId, int duration) {
        initToast(context.getResources().getText(strResId), duration).show();
    }


    /**
     * 显示有image的toast
     *
     * @param tvStr
     * @param imageResource
     * @return
     */
    public static Toast showToastWithImg(final String tvStr, final int imageResource) {
        if (mImgToast == null) {
            mImgToast = new Toast(BaseApplication.getAppContext());
        }
        View view = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout
                .toast_custom, null);
        TextView tv = view.findViewById(R.id.toast_custom_tv);
        tv.setText(TextUtils.isEmpty(tvStr) ? "" : tvStr);
        ImageView iv = view.findViewById(R.id.toast_custom_iv);
        if (imageResource > 0) {
            iv.setVisibility(View.VISIBLE);
            iv.setImageResource(imageResource);
        } else {
            iv.setVisibility(View.GONE);
        }
        mImgToast.setView(view);
        mImgToast.setGravity(Gravity.CENTER, 0, 0);
        mImgToast.show();
        return mImgToast;

    }

    public static Toast showToastWithImgAndSuc(final String tvStr) {
        if (mImgToast == null) {
            mImgToast = new Toast(BaseApplication.getAppContext());
        }
        View view = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout
                .toast_custom_suc, null);
        TextView tv = view.findViewById(R.id.toast_custom_tv);
        tv.setText(TextUtils.isEmpty(tvStr) ? "" : tvStr);
        mImgToast.setView(view);
        mImgToast.setGravity(Gravity.CENTER, 0, 0);
        mImgToast.setDuration(Toast.LENGTH_LONG);
        mImgToast.show();
        return mImgToast;

    }

    public static Toast showErrorToast() {
        if (mImgToast == null) {
            mImgToast = new Toast(BaseApplication.getAppContext());
        }
        View view = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout
                .toast_custom, null);
        TextView tv = view.findViewById(R.id.toast_custom_tv);
        tv.setText(BaseApplication.getAppContext().getResources().getString(R.string.error_toast));
        ImageView iv = view.findViewById(R.id.toast_custom_iv);
        iv.setImageResource(R.mipmap.general_icon_place);
        mImgToast.setView(view);
        mImgToast.setGravity(Gravity.CENTER, 0, 0);
        mImgToast.show();
        return mImgToast;

    }
    public static void showNormalDialog(Context mContext, String msg) {
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(mContext);
        normalDialog.setIcon(R.mipmap.ic_launcher);
        normalDialog.setTitle("温馨提示:");
        normalDialog.setMessage(msg);
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                    }
                });
        // 显示
        normalDialog.show();
    }
    public static Toast showNorToast(final String tvStr) {
        if (mImgToast == null) {
            mImgToast = new Toast(BaseApplication.getAppContext());
        }
        View view = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout
                .new_toast_custom, null);
        TextView tv = view.findViewById(R.id.tv_toast);
        tv.setText(TextUtils.isEmpty(tvStr) ? "" : tvStr);
        mImgToast.setView(view);
        mImgToast.setGravity(Gravity.CENTER, 0, 0);
        mImgToast.setDuration(Toast.LENGTH_LONG);
        mImgToast.show();
        return mImgToast;
    }
}

下面是布局文件

new_toast_custom.xml



    

toast_custom.xml



    

        

            

            
        

    

toast_custom_suc.xml



    

        

            

            
        

    

 

拷贝到项目中即可按需使用

你可能感兴趣的:(工具类)