为了实现底部输入密码框,同时添加遮罩效果 /** * 底部能用输入密码框 * 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)