Toast简单封装

1.在AndroidManifest.XML中声明这个MyApplication

android:name=".MyApplication"

2.自定义Application【系统上下文】


import android.app.Application;
import android.content.Context;

public class MyApplication extends Application {
    /**系统上下文*/
    private static Context mAppContext;

    @Override
    public void onCreate() {
        super.onCreate();
        mAppContext = getApplicationContext();

    }

    /**获取系统上下文:用于ToastUtil类*/
    public static Context getAppContext(){
        return mAppContext;
    }
}

3. Toast的封装


import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;

/**
 * Used 简单的Toast封装类
 */
public class ToastUtil {
    //实现不管我们触发多少次Toast调用,都只会持续一次Toast显示的时长
    private static Toast toast;

    /**
     * 短时间显示Toast【居下】
     * @param msg 显示的内容-字符串
     */
    public static void ToastBelowshow(String msg) {
        if (MyApplication.getAppContext() != null) {
            if (toast == null) {
                toast = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_SHORT);
            } else {
                toast.setText(msg);
            }
            //1、setGravity方法必须放到这里,否则会出现toast始终按照第一次显示的位置进行显示(比如第一次是在底部显示,那么即使设置setGravity在中间,也不管用)
            //2、虽然默认是在底部显示,但是,因为这个工具类实现了中间显示,所以需要还原,还原方式如下:
            toast.setGravity(Gravity.BOTTOM, 0, dip2px(MyApplication.getAppContext(), 64));
            toast.show();
        }
    }

    /**
     * 短时间显示Toast【居中】
     * @param msg 显示的内容-字符串
     */
    public static void ToastCentershow(String msg) {
        if (MyApplication.getAppContext() != null) {
            if (toast == null) {
                toast = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_SHORT);
            } else {
                toast.setText(msg);
            }
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    }

    /**
     * 短时间显示Toast【居上】
     * @param msg 显示的内容-字符串
     */
    public static void ToastTopshow(String msg) {
        if (MyApplication.getAppContext() != null) {
            if (toast == null) {
                toast = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_SHORT);
            } else {
                toast.setText(msg);
            }
            toast.setGravity(Gravity.TOP, 0, 0);
            toast.show();
        }
    }

    /**
     * 长时间显示Toast【居下】
     * @param msg 显示的内容-字符串
     */
    public static void ToastBelowshowLong(String msg) {
        if (MyApplication.getAppContext() != null) {
            if (toast == null) {
                toast = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_LONG);
            } else {
                toast.setText(msg);
            }
            toast.setGravity(Gravity.BOTTOM, 0, dip2px(MyApplication.getAppContext(), 64));
            toast.show();
        }
    }

    /**
     * 长时间显示Toast【居中】
     * @param msg 显示的内容-字符串
     */
    public static void ToastCentershowLong(String msg) {
        if (MyApplication.getAppContext() != null) {
            if (toast == null) {
                toast = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_LONG);
            } else {
                toast.setText(msg);
            }
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    }

    /**
     * 长时间显示Toast【居上】
     * @param msg 显示的内容-字符串
     */
    public static void ToastTopshowLong(String msg) {
        if (MyApplication.getAppContext() != null) {
            if (toast == null) {
                toast = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_LONG);
            } else {
                toast.setText(msg);
            }
            toast.setGravity(Gravity.TOP, 0, 0);
            toast.show();
        }
    }

    /*=================================常用公共方法============================*/
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
    //ToastUtil.ToastTopshow(MyApplication.getAppContext().getResources().getString(R.string.app_name));
    // 如果想要显示Strings.xml文件中的字符串,建议使用MyApplication.getAppContext()
}

猜你喜欢

转载自blog.csdn.net/qq_28845393/article/details/87916114