工具篇——ToastUtil(覆盖上个Toast)

写代码的四点:
     1.明确需求。要做什么?
     2.分析思路。要怎么做?(1,2,3……)
     3.确定步骤。每一个思路要用到哪些语句、方法和对象。
     4.代码实现。用具体的语言代码将思路实现出来。

学习新技术的四点:
     1.该技术是什么?
     2.该技术有什么特点?(使用需注意的方面)
     3.该技术怎么使用?(写Demo)
     4.该技术什么时候用?(在Project中的使用场景 )

----------------------早计划,早准备,早完成。-------------------------

代码如下:

package com.gzqol.xjxh.xjmendianguanli.util;

import android.content.Context;
import android.os.Handler;
import android.widget.Toast;

/**
 * Created by WY on 2017/3/7.
 * 下一个Toast覆盖上一个Toast,防止Toast弹出的时间过长
 */
public class ToastUtil {
    private static Toast mToast;
    private static Handler mHandler = new Handler();
    private static Runnable r = new Runnable() {
        public void run() {
            mToast.cancel();
        }
    };

    public static void makeText(Context mContext, String text) {

        mHandler.removeCallbacks(r);
        if (mToast != null)
            mToast.setText(text);
        else
            mToast = Toast.makeText(mContext, text, Toast.LENGTH_LONG);
        mHandler.postDelayed(r, 2000);

        mToast.show();
    }
}

在项目中的应用:

ToastUtil.makeText(getApplicationContext(), "下一个toast会覆盖上一个的");

猜你喜欢

转载自blog.csdn.net/qq941263013/article/details/80089847