PopupWindow 添加遮罩层

为了实现底部输入密码框,同时添加遮罩效果
/**
 * 底部能用输入密码框
 * by Felix
 */
public class InputPasswordDialog {

    private static PopupWindow popupWindow = null;

    public interface ClickItemCallback {
        public void onItemClick(String text);
    }

    public void showDialog(
            Context context,
            ClickItemCallback clickItemCallback
    ) {
        View popupView = LayoutInflater.from(context).inflate(R.layout.input_password_dialog, null);
        View commonRoot = popupView.findViewById(R.id.layoutRoot);
        commonRoot.setOnClickListener(v -> clickItemCallback.onItemClick(""));


        EditText etPwd = commonRoot.findViewById(R.id.etPwd);

        TextView tvConfirm = commonRoot.findViewById(R.id.btn_confirm);
        tvConfirm.setOnClickListener(v -> {
            String pwd = etPwd.getText().toString().trim();
            if (TextUtils.isEmpty(pwd)) {
                ToastUtils.showToast(R.string.input_pwd_dialog_tip);
                return;
            }
            if (clickItemCallback != null) {
                clickItemCallback.onItemClick(pwd);
                bgAlpha(context, 1.0f);
                onDismiss();
            }
        });

        TextView btn_cancel = commonRoot.findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(v -> {
            bgAlpha(context, 1.0f);
            onDismiss();
        });

        popupView.findViewById(R.id.layoutRoot).setOnClickListener(v -> {
            bgAlpha(context, 1.0f);
            popupWindow.dismiss();
        });

        popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);

        bgAlpha(context, 0.4f);

        popupWindow.setOnDismissListener(() -> {
            bgAlpha(context, 1.0f);
            onDismiss();
        });
        popupWindow.showAtLocation(popupView, Gravity.BOTTOM, 0, 0);
    }

    public static void onDismiss() {
        popupWindow.dismiss();
    }

    private void bgAlpha(Context context, float alpha) {
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = alpha;// 0.0-1.0
        ((Activity) context).getWindow().setAttributes(lp);
    }
}

 

input_password_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutRoot"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:focusable="true"
    android:orientation="vertical">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:text="Please input a password"
        android:textColor="@color/color_333333"
        android:textSize="@dimen/text_20" />

    <EditText
        android:id="@+id/etPwd"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/bg_input_border" />

    <TextView
        android:id="@+id/btn_confirm"
        android:layout_width="match_parent"
        android:layout_height="43dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="31dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/create_wallet_btn_bg_selector"
        android:gravity="center"
        android:text="@string/action_confirm"
        android:textColor="@color/white" />

    <TextView
        android:id="@+id/btn_cancel"
        android:layout_width="match_parent"
        android:layout_height="43dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="@dimen/margin_10"
        android:background="@drawable/create_wallet_disenabled_btn_bg_shape"
        android:gravity="center"
        android:text="@string/action_cancel"
        android:textColor="@color/white" />

</LinearLayout>

 

添加遮罩效果,

private void bgAlpha(Context context, float alpha) {

    WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes(); lp.alpha = alpha;

   // 0.0-1.0  该值越大,就会透明,越小就会有遮罩效果

    ((Activity)context).getWindow().setAttributes(lp);

}

// dismiss
popupWindow.dismiss();
bgAlpha(1.0f)

// show
popupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);
bgAlpha(0.5f)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下代码来实现弹出PopupWindow,并添加背景以及点击时弹窗消失。 首先,在你的布局文件(例如activity_main.xml)中添加一个透明的背景视图: ```xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Your main layout content here --> <View android:id="@+id/background_view" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" /> </FrameLayout> ``` 然后,在你的Activity或Fragment中的代码中,添加以下方法来显示和隐藏PopupWindow: ```java public class MainActivity extends AppCompatActivity { private PopupWindow popupWindow; private View backgroundView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); backgroundView = findViewById(R.id.background_view); // 设置背景点击事件 backgroundView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismissPopupWindow(); } }); // 显示PopupWindow showPopupWindow(); } private void showPopupWindow() { // 创建PopupWindow视图 LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View popupView = inflater.inflate(R.layout.popup_window, null); // 设置PopupWindow的属性 popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 显示PopupWindow popupWindow.showAtLocation(findViewById(R.id.main_layout), Gravity.CENTER, 0, 0); } private void dismissPopupWindow() { // 隐藏PopupWindow if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); } } } ``` 最后,创建一个popup_window.xml资源文件来定义PopupWindow的布局: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/white" android:orientation="vertical" android:padding="16dp"> <!-- Your PopupWindow content here --> </LinearLayout> ``` 这样,当你的Activity或Fragment启动时,PopupWindow将显示在屏幕中央,并且点击背景PopupWindow将被隐藏。请根据你的需要修改布局和样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值