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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/tv_toast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/txt_size_16" android:paddingRight="@dimen/common_margin_20" android:paddingLeft="@dimen/common_margin_20" android:paddingTop="@dimen/common_margin_10" android:paddingBottom="@dimen/common_margin_10" android:background="@drawable/toast_bg" android:text="暂无数据" android:textColor="@color/white"/> </LinearLayout>
toast_custom.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/main_shadow"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:orientation="vertical"> <ImageView android:layout_marginTop="10dp" android:id="@+id/toast_custom_iv" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/ic_empty_picture" android:layout_gravity="center"/> <TextView android:id="@+id/toast_custom_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:textSize="16sp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginBottom="10dp" android:textColor="@color/txt_hint" tools:text="登录成功"/> </LinearLayout> </FrameLayout> </LinearLayout>
toast_custom_suc.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/tv_rounded_corners"> <LinearLayout android:layout_width="160dp" android:layout_height="80dp" android:orientation="vertical"> <ImageView android:layout_marginTop="10dp" android:id="@+id/toast_custom_iv" android:layout_width="30dp" android:layout_height="30dp" android:src="@mipmap/currency_icon_right" android:layout_gravity="center"/> <TextView android:layout_gravity="center_horizontal" android:id="@+id/toast_custom_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:textSize="16sp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginBottom="10dp" android:textColor="@color/txt_hint" android:maxLines="1" android:ellipsize="end" tools:text="登录成功"/> </LinearLayout> </FrameLayout> </LinearLayout>
拷贝到项目中即可按需使用