SoFunction
Updated on 2025-04-10

Android programming method to implement Toast only display the last item

This article describes the method of Android programming to implement Toast to only display the last item. Share it for your reference, as follows:

In Android development, Toast may be used from time to time, but when using Toast, there will be a problem with continuous use, that is, toast is displayed one by one. Instead of directly displaying the last one. Therefore, according to this requirement, the ToastUtil class is now written, and there are three methods to choose from.

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class ToastUtil {
  //Method 1  private static Handler mHandler = new Handler(());
  private static Toast mToast = null;
  private static Object synObject = new Object();
  public static void showToastByThread(Context context, String msg){
    showToastByThread(context, msg, Toast.LENGTH_SHORT);
  }
  public static void showToastByThread(Context context, int msg){
    showToastByThread(context, (msg), Toast.LENGTH_SHORT);
  }
  public static void showToastByThread(final Context context, final CharSequence msg, final int length){
    new Thread(new Runnable() {
      @Override
      public void run() {
        (new Runnable() {
          @Override
          public void run() {
            synchronized (synObject) {
              if (mToast != null){
                (msg);
                (length);
              }else{
                mToast = (context, msg, length);
              }
              ();
            }
          }
        });
      }
    }).start();
  }
  //Method 2: Note that this method cannot be used in child threads anymore  private static long oneTime;
  private static long twoTime;
  private static String oldMsg;
  public static void showToastByTime(Context context, String msg){
    if (mToast == null) {
      mToast = (context, msg, Toast.LENGTH_SHORT);
      ();
      oneTime = ();
    } else {
      twoTime = ();
      if ((oldMsg)){
        if (twoTime-oneTime > Toast.LENGTH_SHORT){
          ();
        }
      } else {
        oldMsg = msg;
        (msg);
        ();
      }
    }
    oneTime = twoTime;
  }
  public static void showToastByTime(Context context, int msg){
    showToastByTime(context, (msg));
  }
  //Method 3  public static TextView mTextView;
  public static WindowManager mWindowManager = null;
  private static Handler mPriHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
      cancelToast();
    }
  };
  public static void showToastByWindow(Context context, String msg){
    mWindowManager = (WindowManager) (Context.WINDOW_SERVICE);
    if (mTextView == null){
      mTextView = new TextView(context);
    }
    (msg);
    (20);
    (0, 0, 0, 30);
    if (() == null){
       params = new ();
      = ;
       = 0.65f;
       = 0;
       = .WRAP_CONTENT;
       = .WRAP_CONTENT;
       = .FLAG_NOT_FOCUSABLE |
          .FLAG_NOT_TOUCHABLE |
          .FLAG_KEEP_SCREEN_ON |
          .FLAG_LAYOUT_IN_SCREEN;
       = ;
       = 0;
      (mTextView, params);
      (101, 1000);
    } else {
      (msg);
      (101);
      (101, 1000);
    }
  }
  public static void cancelToast(){
    if (mTextView != null && () != null){
      (mTextView);
    }
  }
}

For more information about Android related content, please check out the topic of this site:Android layout layout tips summary》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.