Android开发网

首页|Android开发环境|Android开发教程|Android开发视频|Android游戏开发|Android开发实例|Android开发书籍|鸡啄米博客

Android手机卫士(二十一):确认密码对话框编写

    本文接着实现“确认密码”功能,也即是用户以前设置过密码,现在只需要输入确认密码

Android手机卫士(二十一):确认密码对话框编写

  布局文件和《Android 手机卫士--设置密码对话框》中的布局基本类似,所有copy一下,修改一点细节就搞定:

XML/HTML代码
  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.     <TextView  
  8.         style="@style/TitleStyle"  
  9.         android:background="#f00"  
  10.         android:text="确认密码"  
  11.         />  
  12.     <EditText  
  13.         android:id="@+id/et_confirm_psd"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:hint="确认密码"  
  17.         />  
  18.   
  19.     <LinearLayout  
  20.         android:layout_width="match_parent"  
  21.         android:layout_height="wrap_content">  
  22.   
  23.         <Button  
  24.             android:id="@+id/bt_submit"  
  25.             android:layout_width="0dp"  
  26.             android:layout_height="wrap_content"  
  27.             android:layout_weight="1"  
  28.             android:text="确认" />  
  29.   
  30.         <Button  
  31.             android:id="@+id/bt_cancel"  
  32.             android:layout_width="0dp"  
  33.             android:layout_height="wrap_content"  
  34.             android:layout_weight="1"  
  35.             android:text="取消" />  
  36.     </LinearLayout>  
  37.   
  38. </LinearLayout>  

  代码逻辑也基本类似,简单的修改一下

Java代码
  1. /** 
  2.  * 确认密码对话框 
  3.  */  
  4. private void showConfirmPsdDialog() {  
  5.     //需要自己去定义对话框的显示样式,所以要调用dialog.setView(view);  
  6.     Builder builder = new Builder(this);  
  7.     final AlertDialog dialog = builder.create();  
  8.     final View view = inflate(this, R.layout.dialog_confirm_psd, null);  
  9.     //让对话框显示一个自己定义的对话框界面效果  
  10.     dialog.setView(view);  
  11.     dialog.show();  
  12.   
  13.     Button bt_submit = (Button) view.findViewById(R.id.bt_submit);  
  14.     Button bt_cancel = (Button) view.findViewById(R.id.bt_cancel);  
  15.   
  16.     bt_submit.setOnClickListener(new OnClickListener() {  
  17.         @Override  
  18.         public void onClick(View v) {  
  19.             EditText et_confirm_psd = (EditText) view.findViewById(R.id.et_confirm_psd);  
  20.             String confirmPsd = et_confirm_psd.getText().toString();  
  21.             String psd = SpUtil.getString(getApplicationContext(),ConstantValue.MOBILE_SAFE_PSD, "");  
  22.             if(!TextUtils.isEmpty(confirmPsd)){  
  23.                 //进入用户手机防盗模块  
  24.                 if(psd.equals(confirmPsd)) {  
  25.                     Intent intent = new Intent(getApplicationContext(), testActivity.class);  
  26.                     startActivity(intent);  
  27.                     //跳转到新的界面以后需要去隐藏对话框  
  28.                     dialog.dismiss();  
  29.                 } else {  
  30.                     ToastUtil.show(getApplicationContext(),"输入密码错误");  
  31.                 }  
  32.   
  33.             }else{  
  34.                 //提示用户密码输入为空的情况  
  35.                 ToastUtil.show(getApplicationContext(),"请输入密码");  
  36.             }  
  37.         }  
  38.     });  
  39.     bt_cancel.setOnClickListener(new OnClickListener() {  
  40.         @Override  
  41.         public void onClick(View view) {  
  42.             dialog.dismiss();  
  43.         }  
  44.     });  
  45. }

Tags:Dialog | 2017/9/19 | 发表评论

相关文章: