自定义toast工具类

该博客介绍了一个自定义的Toast工具类,包括短时间、长时间显示以及自定义显示时间的Toast方法。类中设置了是否显示Toast的开关,并提供了设置文字大小、颜色以及自定义布局的功能,以提供更丰富的展示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值