public class AlertWindow {
private static final String TAG = AlertWindow.class.getSimpleName();
private TextView mTvCommonTitle;
private TextView mTvCommonContent;
private TextView mTvCommonGo;
private TextView mTvCommonCancel;
private WindowManager mWindowManager;
private WindowManager.LayoutParams mLayoutParams;
/**
* false表示view没有被add
*/
private boolean mAddTag = false;
private View mView;
private OnDialogClickListener mListener;
public AlertWindow(Context context) {
LogUtils.i(TAG, "AlertWindow is init.");
initWindow(context);
}
private void initWindow(Context context) {
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
mLayoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
0, 0,
PixelFormat.TRANSPARENT);
mLayoutParams.gravity = Gravity.CENTER;
mLayoutParams.x = 0;
mLayoutParams.y = 0;
mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
mLayoutParams.type = WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW + 17;
mView = LayoutInflater.from(context).inflate(R.layout.dialog_common_large, null);
mTvCommonTitle = mView.findViewById(R.id.tv_common_title);
mTvCommonContent = mView.findViewById(R.id.tv_common_content);
mTvCommonGo = mView.findViewById(R.id.tv_common_go);
mTvCommonCancel = mView.findViewById(R.id.tv_common_cancel);
mTvCommonGo.setOnClickListener(view -> {
LogUtils.i(TAG, "AlertWindow go is click.");
if (mListener != null) {
mListener.go();
}
hide();
});
mTvCommonCancel.setOnClickListener(view -> {
LogUtils.i(TAG, "AlertWindow cancel is click.");
if (mListener != null) {
mListener.cancel();
}
hide();
});
}
public void hide() {
if (mAddTag) {
LogUtils.i(TAG, "AlertWindow hide has enter.");
mWindowManager.removeView(mView);
mAddTag = false;
}
}
public void show() {
if (!mAddTag) {
LogUtils.i(TAG, "AlertWindow show has enter.");
mWindowManager.addView(mView, mLayoutParams);
mAddTag = true;
}
}
/**
* 设置标题文字
*
* @param title title
*/
public void setCommonTitle(String title) {
LogUtils.i(TAG, "setCommonTitle title = " + title);
if (mTvCommonTitle != null && !TextUtils.isEmpty(title)) {
mTvCommonTitle.setText(title);
}
}
/**
* 设置内容文字
*
* @param text text
*/
public void setCommonContent(String text) {
LogUtils.i(TAG, "setCommonContent text = " + text);
if (mTvCommonContent != null && !TextUtils.isEmpty(text)) {
mTvCommonContent.setText(text);
}
}
/**
* 设置确定按钮文字
*
* @param confirm confirm
*/
public void setCommonGo(String confirm) {
LogUtils.i(TAG, "setCommonGo confirm = " + confirm);
if (mTvCommonGo != null && !TextUtils.isEmpty(confirm)) {
mTvCommonGo.setText(confirm);
}
}
/**
* 设置取消按钮文字
*
* @param cancel cancel
*/
public void setCommonCancel(String cancel) {
LogUtils.i(TAG, "setCommonCancel cancel = " + cancel);
if (mTvCommonCancel != null && !TextUtils.isEmpty(cancel)) {
mTvCommonCancel.setText(cancel);
}
}
public void setOnDialogClickListener(OnDialogClickListener listener) {
this.mListener = listener;
}
public interface OnDialogClickListener {
void go();
void cancel();
}
}
Android 用windowManager弹框
于 2024-08-13 16:16:47 首次发布