showSetPwdDialog--自定义对话框

样式:

布局:

layout

  dialog_set_pwd.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6
 7     <LinearLayout
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content"
10         android:background="#EFEFEF"
11         android:orientation="horizontal"
12         android:padding="10dp" >
13
14         <ImageView
15             android:layout_width="wrap_content"
16             android:layout_height="wrap_content"
17             android:src="@drawable/dialog_title_default_icon" />
18
19         <TextView
20             android:id="@+id/textView1"
21             android:layout_width="wrap_content"
22             android:layout_height="wrap_content"
23             android:layout_marginLeft="5dp"
24             android:text="设置密码"
25             android:textColor="@color/black"
26             android:textSize="18sp" />
27     </LinearLayout>
28
29     <EditText
30         android:id="@+id/et_pwd"
31         android:layout_width="match_parent"
32         android:layout_height="wrap_content"
33         android:layout_margin="5dp"
34         android:hint="请输入密码"
35         android:inputType="textPassword" >
36     </EditText>
37
38     <EditText
39         android:id="@+id/et_pwd_confirm"
40         android:layout_width="match_parent"
41         android:layout_height="wrap_content"
42         android:layout_margin="5dp"
43         android:hint="请再次输入密码"
44         android:inputType="textPassword" />
45
46     <LinearLayout
47         android:layout_width="match_parent"
48         android:layout_height="wrap_content"
49         android:orientation="horizontal"
50         android:padding="10dp" >
51
52         <Button
53             android:id="@+id/btn_ok"
54             android:layout_width="0dp"
55             android:layout_height="wrap_content"
56             android:layout_weight="1"
57             android:background="@drawable/btn_blue_selector"
58             android:text="确定"
59             android:layout_marginRight="5dp"
60             android:textColor="@color/white" />
61
62         <Button
63             android:id="@+id/btn_cancel"
64             android:layout_width="0dp"
65             android:layout_height="wrap_content"
66             android:layout_weight="1"
67             android:background="@drawable/btn_white_selector"
68             android:text="取消"
69             android:textColor="@color/black" />
70     </LinearLayout>
71
72 </LinearLayout>

状态选择器:

drawable

  btn_blue_selector.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android="http://schemas.android.com/apk/res/android">
3
4     <item android:drawable="@drawable/dg_btn_confirm_select" android:state_pressed="true"></item>
5     <item android:drawable="@drawable/dg_btn_confirm_normal"></item>
6
7 </selector>

  btn_white_selector.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android="http://schemas.android.com/apk/res/android">
3
4     <item android:drawable="@drawable/dg_button_cancel_select" android:state_pressed="true"></item>
5     <item android:drawable="@drawable/dg_button_cancel_normal"></item>
6
7 </selector>

引用值

values

  colors.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3
 4     <color name="black">#000</color>
 5     <color name="gray">#a000</color>
 6     <color name="white">#fff</color>
 7     <color name="red">#f00</color>
 8     <color name="shape_setting_normal">#B1D7EE</color>
 9     <color name="shape_setting_pressed">#3C9AD4</color>
10     <color name="blue">#459FD7</color>
11     <color name="light_green">#80f0</color>
12
13 </resources>

代码:

 1     private void showSetPwdDialog() {
 2         AlertDialog.Builder builder = new AlertDialog.Builder(this);
 3         View view = View.inflate(this, R.layout.dialog_set_pwd, null);
 4
 5         Button btnOk = (Button) view.findViewById(R.id.btn_ok);
 6         Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);
 7
 8         final EditText etPwd = (EditText) view.findViewById(R.id.et_pwd);
 9         final EditText etPwdConfirm = (EditText) view
10                 .findViewById(R.id.et_pwd_confirm);
11
12         builder.setView(view);//将当前布局对象设置给dialog
13         final AlertDialog dialog = builder.create();
14
15         btnOk.setOnClickListener(new OnClickListener() {
16
17             @Override
18             public void onClick(View v) {
19                 String pwd = etPwd.getText().toString().trim();
20                 String pwdConfirm = etPwdConfirm.getText().toString().trim();
21
22                 if (TextUtils.isEmpty(pwd) || TextUtils.isEmpty(pwdConfirm)) {
23                     ToastUtils.showToast(getApplicationContext(), "输入内容不能为空!");
24                 } else {
25                     if (pwd.equals(pwdConfirm)) {
26                         System.out.println("登录成功!");
27
28                         //将密码保存在本地sp
29                         PrefUtils.putString(getApplicationContext(),
30                                 GlobalConstants.PREF_PASSWORD,
31                                 MD5Utils.getMd5(pwd));
32
33                         dialog.dismiss();
34
35                         enterLostAndFindPage();
36                     } else {
37                         ToastUtils.showToast(getApplicationContext(),
38                                 "两次密码不一致!");
39                     }
40
41                 }
42             }
43         });
44
45         btnCancel.setOnClickListener(new OnClickListener() {
46
47             @Override
48             public void onClick(View v) {
49                 dialog.dismiss();
50             }
51         });
52
53         dialog.show();
54     }
时间: 2024-08-06 00:47:49

showSetPwdDialog--自定义对话框的相关文章

Android实例-手机安全卫士(十)-自定义对话框

一.目标. 当点击“手机防盗”时弹出自定义的设置密码对话框.如果已经设置密码,则弹出自定义的输入密码对话框. 二.代码实现. 1.在layout文件夹下新建xml(取名setpwddialog.xml)文件用于设置自定义对话框的UI. 新建xml文件代码如下; 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.a

Android实例-手机安全卫士(十一)-自定义对话框点击事件处理

一.目标 在弹出的自定义对话框中,当点击“确认”时,保存密码,关闭对话框:当点击“取消”时关闭对话框.同时,如果不输入密码或输入密码错误则弹出相应的提示. 二.代码实现. 1.在主界面代码类(HomeActivity)中定义设置防盗密码对话框中的对应TextView.Button等成员变量,并在显示设置防盗密码对话框方法(showSetPwdDialog)中通过view.findViewById方法找到对应的组件: 2.为“取消”按钮设置取消操作的监听事件并进行处理.通过setOnClickLi

View(视图)——对话框之自定义对话框

一.自定义对话框 1.不能直接实例化使用 2.使用内部构造器来生成对话框 3.new  AlertDialog.Builder(context)  实例化构造器 1-setTitle (标题) 2-setMessage (消息) 3-按钮 1°确认按钮  setPositiveButton(“文字”,点击事件监听器) 2°否认按钮  setNegativeButton(“文字”,点击事件监听器) 3°中立按钮  setNeutralButton(“文字”,点击事件监听器) 4-show() 创建

Android开发学习笔记-自定义对话框

系统默认的对话框只能显示简单的标题内容以及按钮,而如果想要多现实其他内容则就需要自定义对话框,下面是自定义对话框的方法. 1.先定义对话框的模版 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="300d

android 自定义对话框

新建一个布局文件 my_dialog.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match

Android—关于自定义对话框的工具类

开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函数的参数中,并且是静态,可以用类直接调用此函数. public class MyAutoDialogUtil { private static AlertDialog dialog; /** * * @param context * 上下文 * @param text * 自定义显示的文字 * @p

软键盘挡住自定义对话框中的内容

在自定义的对话框中有EditText,获取焦点的时候弹出软键盘很可能会挡住对话框中的一些按钮或其他内容. 可以在自定义对话框中的oncreate()方法中设置软键盘的模式,如下 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 当软键盘弹出时,弹出

采用建造者模式自定义对话框

1.对话框视图 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="300dip" 4 android:layout_height="240dip" 5 andro

Dialog主题Activity实现自定义对话框效果

想必大家都用过Dialog主题的Activity吧,用它来显示自定义对话框效果绝对是一个非常不错的选择. 即把activity交互界面以Dialog的形式展现出来,Dialog主题的Activity大小将以内容的宽高来决定 <activity android:name=”MainActivity” android:theme=”@android:style/Theme.Dialog”> </activity> 可以看到设置为Theme.Dialog主题的activity显示效果,

安卓应用-自定义对话框

现在要说的是自定义对话框. 常用的对话框有好多网友们整理的资料,对话框大全啊神马的.但是除了常用对话框外,有时候要根据自己的需求定义新的对话框,下面就稍微说一下自定义对话框的一种做法. 1.需要一个对话框内容的xml配置文件game_dialog.xml,这个应该不需要过多解释,基本布局而已. 有几个图片按钮,所以在drawable下面放几张图,嫌麻烦的话,也可以用Button替换,看下效果. 需要说明的是,所有控件的id这里我都用的@id,因为id我都写在了res/values/ids.xml