美文网首页
Android-ToastUtil提示框

Android-ToastUtil提示框

作者: a1a4b0d9e20c | 来源:发表于2020-05-28 11:55 被阅读0次

1、调用方法 R.string.regist_not_empty_pwd:文字描述

ToastUtil.show(getResources().getString(R.string.regist_not_empty_pwd));

ToastUtil.show("登录成功");

2、创建ToastUtil类

public class ToastUtil {

    private static Toast mToast;
    private static int yOffset; // 弹出toast默认的 距底部的距离

    public static void init(Context context){
        if (context == null){
            throw new IllegalArgumentException("Context should not be null");
        }
        if(mToast == null){
            mToast = Toast.makeText(context, null, Toast.LENGTH_SHORT);
            yOffset = mToast.getYOffset();
        }
    }
    
    //这个方法  选择性使用
    public static void show(int resId){
        show(BaseApplication.getContext().getResources().getString(resId));
    }

    //常用方法
    public static void show(String content){
        show(content, Gravity.BOTTOM);
    }

    public static void show(String content, int gravity){
        show(content,gravity,Toast.LENGTH_SHORT);
    }

    public static void show(String content, int gravity, int duration){
        if(mToast == null) init(BaseApplication.getContext());
        mToast.setText(content);
        mToast.setDuration(duration);
        mToast.setGravity(gravity,0,yOffset);
        mToast.show();
    }
}

相关文章

网友评论

      本文标题:Android-ToastUtil提示框

      本文链接:https://www.haomeiwen.com/subject/vvboahtx.html