封装的Toast
文章目录
长显示用PopupWindow
短显示用Dialog
- 短显示Dialog:
public class ToastDialog extends Dialog {
private TextView toastTv;
private ImageView cautiousIv;
public ToastDialog(Context context, String msg) {
super(context, R.style.toastDialog);
View view = LayoutInflater.from(context).inflate(R.layout.layout_toast, null);
setCanceledOnTouchOutside(false);
setCancelable(true);
toastTv = view.findViewById(R.id.tv_toast);
cautiousIv = view.findViewById(R.id.iv_cautious);
toastTv.setText(msg);
setContentView(view);
}
public void showUp(String level, ToastDialog toastDialog) {
if (level.equals("e") || level.equals("E") || level.equals("error")) {
cautiousIv.setVisibility(View.VISIBLE);
} else if (level.equals("w") || level.equals("W") || level.equals("warning")) {
cautiousIv.setVisibility(View.VISIBLE);
} else if (level.equals("s") || level.equals("S") || level.equals("success")) {
// toastTv.setVisibility(View.VISIBLE);
} else if (level.equals("i") || level.equals("I") || level.equals("info")) {
// toastTv.setVisibility(View.VISIBLE);
} else if (level.equals("n") || level.equals("N") || level.equals("normal")) {
cautiousIv.setVisibility(View.GONE);
} else {
cautiousIv.setVisibility(View.GONE);
}
Window window = toastDialog.getWindow();
WindowManager.LayoutParams wl = window.getAttributes();
wl.x = 0;//设置x坐标
wl.y = UIUtils.dip2px(getContext(), 105);//设置y坐标
wl.width = WindowManager.LayoutParams.WRAP_CONTENT;
wl.gravity = Gravity.BOTTOM;
window.setAttributes(wl);
toastDialog.show();
}
public void showUp(View view, ToastDialog toastDialog) {
Window window = toastDialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
//获取通知栏高度 重要的在这,获取到通知栏高度
int notificationBar = Resources.getSystem().getDimensionPixelSize(
Resources.getSystem().getIdentifier("status_bar_height",
"dimen", "android"));
//获取控件 textview 的绝对坐标,( y 轴坐标是控件上部到屏幕最顶部(不包括控件本身))
//location [0] 为x绝对坐标;location [1] 为y绝对坐标
int[] location = new int[2];
view.getLocationInWindow(location); //获取在当前窗体内的绝对坐标
view.getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标
wlp.x = 0; //对 dialog 设置 x 轴坐标
wlp.y = location[1] - view.getHeight() - notificationBar + UIUtils.dip2px(view.getContext(), 3); //对dialog设置y轴坐标
wlp.gravity = Gravity.TOP;
wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
System.out.println("height" + wlp.y);
window.setAttributes(wlp);
toastDialog.show();//显示对话框
}
public void onLongDimiss(ToastDialog toast) {
toast.dismiss();
}
public void onShortDimiss(final ToastDialog toast) {
CountDownTimer timer = new CountDownTimer(2000, 10) {
@Override
public void onTick(long l) {
}
@Override
public void onFinish() {
if (toast != null && toast.isShowing()) {
toast.dismiss();
}
}
};
timer.start();
}}
2.长显示PopupWindow:
public class ToastPopupWindow extends PopupWindow {
private int popupWidth;
private int popupHeight;
private TextView toastTv;
public ToastPopupWindow(Context context, String msg) {
View view = LayoutInflater.from(context).inflate(R.layout.layout_toast, null);
toastTv = view.findViewById(R.id.tv_toast);
toastTv.setText(msg);
setContentView(view);
setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
setBackgroundDrawable(new ColorDrawable(0x00000000));
setOutsideTouchable(false);
setTouchable(false);
setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
//获取自身的长宽高
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
popupHeight = view.getMeasuredHeight();
popupWidth = view.getMeasuredWidth();
setAnimationStyle(R.style.popupAnimation);// activity.getWindow()
}
public void showUp(View v, ToastPopupWindow toastPopupWindow) {
//获取需要在其上方显示的控件的位置信息
int[] location = new int[2];
v.getLocationOnScreen(location);
toastPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2)
- toastPopupWindow.popupWidth / 2, (location[1] - toastPopupWindow.popupHeight) - UIUtils.dip2px(v.getContext(),3));
}
public void showCenter(View v, ToastPopupWindow toastPopupWindow) {
toastPopupWindow.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_VERTICAL, 0, UIUtils.dip2px(v.getContext(), 50));
}
public void onLongDimiss(ToastPopupWindow toastPopupWindow) {
toastPopupWindow.dismiss();
}
public void onShortDimiss(final ToastPopupWindow toastPopupWindow) {
CountDownTimer timer = new CountDownTimer(2000, 10) {
@Override
public void onTick(long l) {
}
@Override
public void onFinish() {
if (toastPopupWindow != null && toastPopupWindow.isShowing())
toastPopupWindow.dismiss();
}
};
timer.start();
}}

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论