zoukankan      html  css  js  c++  java
  • 初识安卓之对应用中的某个模块单独进行打开时的密码验证

    1.初次使用时必须先设置一个密码,通过弹出对话框的形式,一个标准的设置密码对话框的布局文件如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="300dip"
        android:layout_height="wrap_content"
    //将对话框默认的黑色背景设置为白色,但这样还不能解决对话框距离上下左右表框间的黑色,这需要在代码里设置
    //对话框四周与边缘的距离为0,需要使用代码来实现
        android:background="#ffffff"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="300dip"
            android:layout_height="45dip"
            android:background="#355E9E"
    //同时满足水平和垂直居中显示
            android:gravity="center"
            android:text="请设置密码"
            android:textColor="#EBC950"
            android:textSize="23dip" />
    
        <EditText
            android:id="@+id/et_first_entry_pwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入密码"
            android:inputType="textPassword" >
        </EditText>
    
        <EditText
           android:id="@+id/et_first_entry_pwd_confirm"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请再次输入密码"
            android:inputType="textPassword" />
    
        <LinearLayout
            android:layout_width="300dip"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/bt_first_entry_ok"
                
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="确定" />
    
            <Button
                 android:id="@+id/bt_first_entry_cancle"
                android:layout_marginLeft="3dip"
                
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="取消" />
        </LinearLayout>
    
    </LinearLayout>
    2.第二次以后登录时,只需要验证密码就可以了,标准的密码验证布局文件如下:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="300dip"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="300dip"
            android:layout_height="45dip"
            android:background="#355E9E"
            android:gravity="center"
            android:text="请输入密码"
            android:textColor="#EBC950"
            android:textSize="23dip" />
    
        <EditText
            android:id="@+id/et_normal_entry_pwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入密码"
            android:inputType="textPassword" >
        </EditText>
    
       
    
        <LinearLayout
            android:layout_width="300dip"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/bt_normal_entry_ok"
               
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="确定" />
    
            <Button
                 android:id="@+id/bt_normal_entry_cancle"
                android:layout_marginLeft="3dip"
                
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="取消" />
        </LinearLayout>
    
    </LinearLayout>
    3.设置点击或者触摸的事件监听,需要主函数实现 OnClickListener接口,这样可方便的复写点击事件
    gv_home.setOnItemClickListener(new OnItemClickListener() { 
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    Intent intent;
                    switch (position) {
           //如果点击的是需要密码验证的模块则判断,0是模块的编号角标
                    case 0:
                        //判断用户是否设置过密码.
                        if(isSetupPwd()){
                            showNormalEntryDialog();
                        }else{
                            showFirstEntryDialog();
                        }
                        break;
                    case 8:
                        intent = new Intent(HomeActivity.this,SettingActivity.class);
                        startActivity(intent);
                        break;
                    }
                }
            });
    
    
    /**
         * 第一次进入弹出注册密码对话框,获取到该对话框上的所有控件对象,给所有按钮添加监听
         */
        private EditText et_first_entry_pwd;
        private EditText et_first_entry_pwd_confirm;
        private Button bt_first_entry_ok;
        private Button bt_first_entry_cancle;
        private AlertDialog dialog;
        protected void showFirstEntryDialog() {
            AlertDialog.Builder builder =new Builder(this);
            View view = View.inflate(this, R.layout.dialog_first_entry, null);
            et_first_entry_pwd = (EditText) view.findViewById(R.id.et_first_entry_pwd);
            et_first_entry_pwd_confirm = (EditText) view.findViewById(R.id.et_first_entry_pwd_confirm);
            bt_first_entry_cancle = (Button)view.findViewById(R.id.bt_first_entry_cancle);
            bt_first_entry_ok = (Button)view.findViewById(R.id.bt_first_entry_ok);
            bt_first_entry_ok.setOnClickListener(this);
            bt_first_entry_cancle.setOnClickListener(this);
    //消除对话框四周与边缘间黑色填充背景的方法:将距离四周的值设为0
            dialog = builder.create();
            dialog.setView(view, 0, 0, 0, 0);
            dialog.show();
            
        }
    
        /**
         * 第二次以后,正常进入密码验证对话框
         */
        private EditText et_normal_entry_pwd;
        private Button bt_normal_entry_ok;
        private Button bt_normal_entry_cancle;
        protected void showNormalEntryDialog() {
            AlertDialog.Builder builder =new Builder(this);
            View view = View.inflate(this, R.layout.dialog_normal_entry, null);
            et_normal_entry_pwd = (EditText) view.findViewById(R.id.et_normal_entry_pwd);
            bt_normal_entry_cancle = (Button)view.findViewById(R.id.bt_normal_entry_cancle);
            bt_normal_entry_ok = (Button)view.findViewById(R.id.bt_normal_entry_ok);
            bt_normal_entry_ok.setOnClickListener(this);
            bt_normal_entry_cancle.setOnClickListener(this);
            dialog = builder.create();
            dialog.setView(view, 0, 0, 0, 0);
            dialog.show();
            
        }
      //令牌:判断是否已注册过密码,若注册过,sp里面有相应的key、values记录
        protected boolean isSetupPwd() {
            String pwd = sp.getString("pwd", "");
            if(TextUtils.isEmpty(pwd)){
                return false;
            }else{
                return true;
            }
        }
    //按钮点击事件的逻辑处理,只要是判断密码正确性
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.bt_normal_entry_cancle:
                dialog.dismiss();
                break;
            case R.id.bt_normal_entry_ok:
                String enter_pwd = et_normal_entry_pwd.getText().toString().trim();
                if(TextUtils.isEmpty(enter_pwd)){
                    Toast.makeText(this, "请输入密码", 0).show();
                    return;
                }
                String saved_pwd = sp.getString("pwd", "");
                if(saved_pwd.equals(enter_pwd)){
          //密码正确则可以进入模块内部了

                 Intent intent = new Intent(this,LostFindActivity.class);
                    startActivity(intent);

                    dialog.dismiss();
                }else{
                    Toast.makeText(this, "密码错误", 0).show();
                    return;
                }
                break;
            case R.id.bt_first_entry_cancle:
                dialog.dismiss();
                break;
    
            case R.id.bt_first_entry_ok:
                String pwd = et_first_entry_pwd.getText().toString().trim();
                String pwd_confirm = et_first_entry_pwd_confirm.getText().toString().trim();
                if(TextUtils.isEmpty(pwd)||TextUtils.isEmpty(pwd_confirm)){
                    Toast.makeText(this, "密码不能为空", 0).show();
                    return;
                }
                if(!pwd.equals(pwd_confirm)){
                    Toast.makeText(this, "两次密码不一致", 0).show();
                    return;
                }
    //利用sp把设置好的密码存起来
                Editor editor = sp.edit();
                editor.putString("pwd", pwd);
                editor.commit();
                dialog.dismiss();
                break;
            }
        }
  • 相关阅读:
    const对象 不能调用非const修饰的成员函数
    Sqlserver 查询 临时字段
    win7 VC6.0 安装 fatal error C1083: Cannot open include file: 'Iphlpapi.h': No such file or directory
    RtlWerpReportException failed with status code :-1073741823
    MFC 去掉CWnd的边框
    阿里云 linux 找回mysql root密码
    IE 内使用ActiveX,写注册表被重定向到如下注册表
    mysqli得到记录数量
    error C4996 The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name
    在MFC主对话框OnInitDialog()中弹出对话框
  • 原文地址:https://www.cnblogs.com/94007boy/p/2821045.html
Copyright © 2011-2022 走看看