Android--万能自定义弹窗

来源:互联网 发布:淘宝750促销模板代码 编辑:程序博客网 时间:2024/06/05 10:29

使用的是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));                }            }        });    }
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 简单瘦腿法 简单十字绣 自考简单吗 简单鸡尾酒 简单防身术 简单婚纱照 简单动作 简单的心 简单的制作 简单麦词 简单程序 越简单 简单的意思 简单美 简单易 简单erp 简单够 简单是美 简单问题 简单的办法 千纸鹤简单 简单做法 简单挂衣柜 简单的要求 简单seo 国庆手抄报简单 简单是什么意思 胃涨怎么办简单的办法 简单压力容器 简单艾灸不生病 简单的去雀斑方法 去头皮屑的简单方法 简单易学的现代舞 毛坯房简单装饰 简单的山水画 简单好玩的手游 怎么去汗毛简单的方法 去眼袋的简单方法 怎样简单瘦腿 瘦腿的简单方法 学习简单英语