public class ToastUtils { private static long lastShowTime = 0l; private ToastUtils() { /* cannot be instantiated 无法 实例化*/ throw new UnsupportedOperationException("cannot be instantiated"); // 抛出异常 } public static boolean isShow = true; /** * 短时间显示Toast * * @param context * @param message */ public static void showShort(Context context, CharSequence message) { if (isShow){ // Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); // 纯文本提示框 (居中) Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER,0,0); LinearLayout linearLayout = (LinearLayout) toast.getView(); TextView messageTextView = (TextView) linearLayout.getChildAt(0); messageTextView.setTextSize(25); messageTextView.setTextColor(Color.parseColor("#FFFFFF")); toast.show(); } } /** * 短时间显示Toast * * @param context * @param message */ public static void showShort(Context context, int message) { if (isShow) Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); } /** * 长时间显示Toast * * @param context * @param message */ // 警告 public static void warnShowLong(Context context, CharSequence message) { if (isShow){ long curShowTime = System.currentTimeMillis(); // 自定义带图标 弹窗样式 View toastView = LayoutInflater.from(context).inflate(R.layout.toast_tips_layout, null); LinearLayout linearLayout = (LinearLayout)toastView.findViewById(R.id.toast_linear); TextView textView = (TextView)toastView.findViewById(R.id.tv_toast_tips); int width = ScreenUtils.getWidth(context); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width * 5/10, ViewGroup.LayoutParams.WRAP_CONTENT); linearLayout.setLayoutParams(layoutParams); textView.setText(message); Toast toast = new Toast(context); toast.setDuration(Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.setView(toastView); toast.show(); } } // 解决 重复显示问题 // public static Toast currentToast = null; // public static void warnShowLong(Context context, CharSequence message) { // if (isShow) { // if (currentToast != null) { // currentToast.cancel(); // } // long curShowTime = System.currentTimeMillis(); // View toastView = LayoutInflater.from(context).inflate(R.layout.toast_tips_layout, null); // LinearLayout linearLayout = (LinearLayout) toastView.findViewById(R.id.toast_linear); // TextView textView = (TextView) toastView.findViewById(R.id.tv_toast_tips); // int width = ScreenUtils.getWidth(context); // LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width * 5 / 10, ViewGroup.LayoutParams.WRAP_CONTENT); // linearLayout.setLayoutParams(layoutParams); // textView.setText(message); // // Toast toast = new Toast(context); // toast.setDuration(Toast.LENGTH_LONG); // toast.setGravity(Gravity.CENTER, 0, 0); // toast.setView(toastView); // toast.show(); // currentToast = toast; // } // } // 成功 public static void successShowLong(Context context,CharSequence message){ if (isShow){ View toastView = LayoutInflater.from(context).inflate(R.layout.toast_success_layout, null); LinearLayout linearLayout = (LinearLayout)toastView.findViewById(R.id.toast_linear_success); TextView textView = (TextView)toastView.findViewById(R.id.tv_toast_success); int width = ScreenUtils.getWidth(context); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width * 5/10, ViewGroup.LayoutParams.WRAP_CONTENT); linearLayout.setLayoutParams(layoutParams); textView.setText(message); Toast toast = new Toast(context); toast.setDuration(Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.setView(toastView); toast.show(); } } /** * 长时间显示Toast * * @param context * @param message */ public static void showLong(Context context, int message) { if (isShow) Toast.makeText(context, message, Toast.LENGTH_LONG).show(); } /** * 自定义显示Toast时间 * * @param context * @param message * @param duration */ public static void show(Context context, CharSequence message, int duration) { if (isShow) Toast.makeText(context, message, duration).show(); } /** * 自定义显示Toast时间 * * @param context * @param message * @param duration */ public static void show(Context context, int message, int duration) { if (isShow) Toast.makeText(context, message, duration).show(); } public static void showUI(final Activity activity, final String message, final int duration) { if (isShow) activity.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(activity, message, duration).show(); } }); } }
xml文件:
toast_tips_lay.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:translationZ="3dp" android:elevation="3dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_gravity="center" android:gravity="center" android:background="@drawable/toast_tips_background" android:id="@+id/toast_linear"> <TextView android:id="@+id/tv_toast_tips" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提示" android:typeface="monospace" android:layout_marginTop="20dp" android:textSize="30dp" android:textStyle="bold" android:textColor="@color/white" android:layout_marginBottom="20dp" android:drawableLeft="@mipmap/jinggao" android:layout_marginStart="20sp" android:drawablePadding="20dp" android:layout_marginEnd="20dp"/> </LinearLayout>toast_tips_background.xml<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/sandybrown"/> <corners android:radius="60dp"/> <!-- 设置圆角大小 --> <stroke android:width="3dp" android:color="@color/sandybrown"/> </shape>