Android自定义dialog弹出布局

来源:互联网 发布:手机计算道路软件 编辑:程序博客网 时间:2024/05/16 07:31

定义一个弹窗布局

首先定义一个dailog_set_password.xml布局文件。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#fff"    android:orientation="vertical" >    <TextView        android:id="@+id/textView1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#66ff6600"        android:gravity="center"        android:padding="10dp"        android:text="设置密码"        android:textColor="@color/black"        android:textSize="20sp" />    <EditText        android:id="@+id/et_password"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入密码"        android:inputType="textPassword" >    </EditText>    <EditText        android:id="@+id/et_password_confirm"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请再次输入密码"        android:inputType="textPassword" >    </EditText>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <Button            android:id="@+id/btn_ok"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="确定" />        <Button            android:id="@+id/btn_cancel"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="取消" />    </LinearLayout></LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

显示这个布局文件

private void showPasswordSetDailog() {        AlertDialog.Builder builder = new AlertDialog.Builder(this);        final AlertDialog dialog = builder.create();        View view = View.inflate(this, R.layout.dailog_set_password, null);        // dialog.setView(view);// 将自定义的布局文件设置给dialog        dialog.setView(view, 0, 0, 0, 0);// 设置边距为0,保证在2.x的版本上运行没问题        final EditText etPassword = (EditText) view                .findViewById(R.id.et_password);        final EditText etPasswordConfirm = (EditText) view                .findViewById(R.id.et_password_confirm);        Button btnOK = (Button) view.findViewById(R.id.btn_ok);        Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);        btnOK.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                String password = etPassword.getText().toString();                String passwordConfirm = etPasswordConfirm.getText().toString();                // password!=null && !password.equals("")                if (!TextUtils.isEmpty(password) && !passwordConfirm.isEmpty()) {                    if (password.equals(passwordConfirm)) {                        // Toast.makeText(HomeActivity.this, "登录成功!",                        // Toast.LENGTH_SHORT).show();                        // 将密码保存起来                        mPref.edit()                                .putString("password",                                        MD5Utils.encode(password)).commit();                        dialog.dismiss();                        // 跳转到手机防盗页                        startActivity(new Intent(HomeActivity.this,                                LostFindActivity.class));                    } else {                        Toast.makeText(HomeActivity.this, "两次密码不一致!",                                Toast.LENGTH_SHORT).show();                    }                } else {                    Toast.makeText(HomeActivity.this, "输入框内容不能为空!",                            Toast.LENGTH_SHORT).show();                }            }        });        btnCancel.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                dialog.dismiss();// 隐藏dialog            }        });        dialog.show();    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
0 0
原创粉丝点击