PopupWindow弹窗,弹窗外部不可点击

弹窗展示
Activity.java中代码
//PopupWindow弹窗
private void showNoneEffect() {
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View vPopupWindow = inflater.inflate(R.layout.popupwindow, null, false);//引入弹窗布局
    popupWindow = new PopupWindow(vPopupWindow, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
    //点击外部弹出不消失
    popupWindow.setFocusable(false);
    popupWindow.setOutsideTouchable(false);
    //设置透明背景布局
    setTransparentBg();

    popu_sj_vip = vPopupWindow.findViewById(R.id.popu_sj_vip);
    popu_sj_vip.setOnClickListener(this);

    //引入依附的布局
    View parentView = LayoutInflater.from(this).inflate(R.layout.activity_setting, null);
    //相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
    popupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);
}
private void setTransparentBg() {
    // 设置背景颜色变暗
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.alpha = 0.7f;//调节透明度
    getWindow().setAttributes(lp);
    //监听弹窗
    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            //弹窗关闭  dismiss()时恢复原样
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.alpha = 1f;
            getWindow().setAttributes(lp);
        }
    });
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    //拦截弹窗外部点击事件
    if (popupWindow != null && popupWindow.isShowing()) {
        return false;
    }
    return super.dispatchTouchEvent(ev);
}

弹窗布局文件popupwindow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center"
    android:paddingTop="28dp"
    android:paddingBottom="40dp"
    android:paddingLeft="56dp"
    android:paddingRight="56dp"
    android:background="@drawable/radius_border">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="假密码"
        android:textColor="#ff000000"
        android:textSize="19sp"
        />
    <TextView
        android:layout_marginTop="36dp"
        android:layout_width="164dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="这是会员功能,免费解锁请升级到会员"
        android:textColor="#ff333333"
        android:textSize="15sp"
        />

    <LinearLayout
        android:id="@+id/popu_sj_vip"
        android:layout_width="160dp"
        android:layout_height="35dp"
        android:layout_marginTop="40dp"
        android:orientation="vertical"
        android:background="@drawable/btn_bg"
        android:gravity="center">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="升级到会员"
            android:textColor="#ffffffff"
            android:textSize="16sp"
            />
    </LinearLayout>



</LinearLayout>

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现该功能需要先创建一个按钮,然后在按钮的点击事件中创建一个 PopupWindow 对象。在 PopupWindow 中,可以设置一个布局文件,该文件会展示在弹窗中。接下来,可以为弹窗中的按钮设置一个点击事件,通过点击事件再创建一个 PopupWindow 对象,展示新的弹窗层。 具体实现步骤如下: 1. 创建一个按钮,设置按钮的点击事件 2. 在点击事件中使用 PopupWindow 类创建一个弹窗对象,并且设置它的布局文件 3. 获取布局文件中的子视图,并在子视图中为按钮设置点击事件 4. 在按钮的点击事件中创建一个新的 PopupWindow 对象,然后为它设置另一个布局文件,展示新的弹窗层 关于具体的代码实现,你可以参考下面的示例代码: ``` kotlin // 创建一个按钮 val button = Button(this) button.text = "点击弹窗" button.setOnClickListener(View.OnClickListener { // 创建一个 PopupWindow 对象 val popupWindow = PopupWindow(this) // 设置弹窗的布局文件 val view = layoutInflater.inflate(R.layout.popup_window, null) popupWindow.contentView = view // 获取布局文件中的子视图 val popupButton = view.findViewById<Button>(R.id.popup_button) popupButton.setOnClickListener(View.OnClickListener { // 创建一个新的 PopupWindow 对象 val popupWindow2 = PopupWindow(this) // 设置弹窗的布局文件 val view2 = layoutInflater.inflate(R.layout.popup_window_2, null) popupWindow2.contentView = view2 // 显示弹窗 popupWindow2.showAsDropDown(popupButton) }) // 显示弹窗 popupWindow.showAsDropDown(button) }) ``` 在代码中,首先创建一个按钮,并设置它的点击事件。在点击事件中,创建了一个 PopupWindow 对象,然后为它设置了一个布局文件。接着,使用 findViewById() 方法获取布局文件中的子视图,并为它设置了一个点击事件。在点击事件中,又创建了一个新的 PopupWindow 对象,并设置了一个新的布局文件。最后,分别通过 showAsDropDown() 方法显示了两个弹窗层。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值