Android--万能自定义弹窗

使用的是AlertDialog

自定义弹窗呢,就是一个界面放在了AlertDialog容器弹框上。

第一步:写好你的界面 (在layout下创建system_admin_psw_alert_dialog.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:padding="10dp"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginBottom="10dp"
        >

        <ImageView
            android:id="@+id/system_admin_psw_alert_img"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/warn"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:text="@string/warning"
            android:textColor="#f00"
            android:textSize="25sp"
            android:layout_marginLeft="6dp"
            android:layout_toRightOf="@+id/system_admin_psw_alert_img"
            />
        <Button
            android:id="@+id/system_admin_psw_alert_close"
            android:layout_width="22dp"
            android:layout_height="22dp"
            android:background="@mipmap/close_icon"
            android:layout_alignParentRight="true"
            />
    </RelativeLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#f00"
        android:textSize="18sp"
        android:padding="8dp"
        android:lineSpacingExtra="8dp"
        android:text="@string/set_debug_info"
        />

    <EditText
        android:id="@+id/sap_alert_dialog_edt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:maxLength="6"
        android:digits="1234567890"
        android:singleLine="true"
        android:layout_margin="16dp"
        android:hint="请输入主管密码"
        android:textColorHint="#ccc"
        android:textColor="#000"
        android:background="@mipmap/sys_login_psw"
    />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        >
        <Button
            android:id="@+id/sap_alert_dialog_cancel"
            android:layout_width="wrap_content"
            android:layout_height="34dp"
            android:background="#f00"
            android:text="关闭"
            android:layout_marginLeft="20dp"
            />
        <Button
            android:id="@+id/sap_alert_dialog_ok"
            android:layout_width="wrap_content"
            android:layout_height="34dp"
            android:background="#f00"
            android:text="开启"
            android:layout_alignParentRight="true"
            android:layout_marginRight="20dp"
            />
    </RelativeLayout>

</LinearLayout>

2、在一个Activity界面上设置按钮 然后触发弹框出现
再触发时间里写 showSetDeBugDialog()方法

 private void showSetDeBugDialog() {
        AlertDialog.Builder setDeBugDialog = new AlertDialog.Builder(this);
        //获取界面
        View dialogView = LayoutInflater.from(this).inflate(R.layout.system_admin_psw_alert_dialog, null);
        //将界面填充到AlertDiaLog容器
        setDeBugDialog.setView(dialogView);
        //初始化控件
        final EditText adminPwd = (EditText) dialogView.findViewById(R.id.sap_alert_dialog_edt);
        Button okButton = (Button) dialogView.findViewById(R.id.sap_alert_dialog_ok);
    //取消点击外部消失弹窗
        setDeBugDialog.setCancelable(false);
        //创建AlertDiaLog
        setDeBugDialog.create();
        //AlertDiaLog显示
        final AlertDialog customAlert = setDeBugDialog.show();
    //设置自定义界面的点击事件逻辑        dialogView.findViewById(R.id.system_admin_psw_alert_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                customAlert.dismiss();
            }
        });
        okButton.setOnClickListener(new android.view.View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str = adminPwd.getText().toString();
                if (str.equals("888888")) {
                    ToastUtil.toast("开启");
                    editor.putString("univ_set_debug", "1");
                    editor.apply();
                    setDebugbtn.setText(getString(R.string.debug_open));
                    customAlert.dismiss();
                }else{
                    ToastUtil.toast(getString(R.string.login_pwd_fail));
                }
            }
        });
        dialogView.findViewById(R.id.sap_alert_dialog_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str = adminPwd.getText().toString();
                if (str.equals("888888")) {
                    ToastUtil.toast("关闭");
                    editor.putString("univ_set_debug", "0");
                    editor.apply();
                    setDebugbtn.setText(getString(R.string.debug_close));
                    customAlert.dismiss();
                }else{
                    ToastUtil.toast(getString(R.string.login_pwd_fail));
                }
            }
        });


    }

猜你喜欢

转载自blog.csdn.net/mr_chenxu/article/details/78894533