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()
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值