Android 用windowManager弹框

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

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值