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();
}
}
网友评论