Toast只显示最后一条

在做Android开发中,时不时的可能会用到Toast,但用Toast的时候,连续使用会存在一个问题,就是一条条显示Toast。而不是直接显示最后一条。因此,根据此需求,现在写了ToastUtil这个类,该类中有三个方法供选择。

ToastUtil.java

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

public class ToastUtil {

    //方法一
    private static Handler mHandler = new Handler(Looper.getMainLooper());
    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, context.getText(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() {
                mHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        synchronized (synObject) {
                            if (mToast != null){
                                mToast.setText(msg);
                                mToast.setDuration(length);
                            }else{
                                mToast = Toast.makeText(context, msg, length);
                            }

                            mToast.show();
                        }
                    }
                });
            }
        }).start();
    }

    //方法二:注意此方法不能再子线程中使用
    private static long oneTime;
    private static long twoTime;
    private static String oldMsg;

    public static void showToastByTime(Context context, String msg){
        if (mToast == null) {
            mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
            mToast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (msg.equals(oldMsg)){
                if (twoTime-oneTime > Toast.LENGTH_SHORT){
                    mToast.show();
                }
            } else {
                oldMsg = msg;
                mToast.setText(msg);
                mToast.show();
            }
        }

        oneTime = twoTime;
    }

    public static void showToastByTime(Context context, int msg){
        showToastByTime(context, context.getString(msg));
    }

    //方法三
    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.getSystemService(Context.WINDOW_SERVICE);

        if (mTextView == null){
            mTextView = new TextView(context);
        }
        mTextView.setText(msg);
        mTextView.setTextSize(20);
        mTextView.setPadding(0, 0, 0, 30);

        if (mTextView.getParent() == null){
            WindowManager.LayoutParams params = new WindowManager.LayoutParams();
            params.gravity= Gravity.BOTTOM;
            params.alpha = 0.65f;
            params.x = 0;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            params.width = WindowManager.LayoutParams.WRAP_CONTENT;
            params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                    WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
                    WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
            params.format = PixelFormat.TRANSLUCENT;
            params.windowAnimations = 0;
            mWindowManager.addView(mTextView, params);

            mPriHandler.sendEmptyMessageDelayed(101, 1000);
        } else {
            mTextView.setText(msg);

            mPriHandler.removeMessages(101);
            mPriHandler.sendEmptyMessageDelayed(101, 1000);
        }
    }

    public static void cancelToast(){
        if (mTextView != null && mTextView.getParent() != null){
            mWindowManager.removeView(mTextView);
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值