封装好的一个的Toast工具类(可以直接调用)

来源:互联网 发布:网络信息安全宣传标语 编辑:程序博客网 时间:2024/05/01 08:54







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

/**
 * Created by xue on 2017/8/4.
 */

public class ToastUtil {

    private static Toast sToast;
    private static Handler handler = new Handler(Looper.getMainLooper());

    public static void showToast(final Context context, final String msg) {

        if (sToast == null) {
            if (Looper.getMainLooper()==Looper.myLooper()){
                initToast(context, msg);
            }else {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        initToast(context, msg);
                    }
                });
            }
        }
        //判断当前代码是否是主线程
        if (Looper.myLooper() == Looper.getMainLooper()) {
            sToast.setText(msg);
            sToast.show();
        } else {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    sToast.setText(msg);
                    sToast.show();
                }
            });
        }
    }

    private static void initToast(Context context, String msg) {
        sToast = Toast.makeText(context.getApplicationContext(), msg, Toast.LENGTH_SHORT);
        sToast.setText(msg);
    }
}








阅读全文
0 0
原创粉丝点击