《Android项目实战-博学谷》设置密保&找回密码

OliverLau 2019-06-26

设置密保&找回密码界面

思路

由于设置密保和找回密码两个界面十分相似,代码逻辑也十分相似,因此这两个界面可以使用同一个布局文件和同一个
Activity 来处理,首先导入所需图片资源文件,再创建 FindPswActivity ,将布局改为 LinearLayout

具体代码如下:

activity_find_psw.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="@drawable/login_bg"
    android:orientation="vertical">

    <include layout="@layout/main_title_bar" />

    <!--先将设置密保部分隐藏,android:visibility="gone"-->

    <TextView
        android:id="@+id/tv_user_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="35dp"
        android:text="您的用户名是?"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        android:visibility="gone" />

    <EditText
        android:id="@+id/et_user_name"
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/find_psw_icon"
        android:hint="请输入您的用户名"
        android:paddingLeft="8dp"
        android:singleLine="true"
        android:textColor="#000000"
        android:textColorHint="#a3a3a3"
        android:visibility="gone" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="15dp"
        android:text="您的姓名是?"
        android:textColor="@android:color/white"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/et_validate_name"
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/find_psw_icon"
        android:hint="请输入要验证的姓名"
        android:paddingLeft="8dp"
        android:singleLine="true"
        android:textColor="#000000"
        android:textColorHint="#a3a3a3" />

    <TextView
        android:id="@+id/tv_reset_psw"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="10dp"
        android:gravity="center_vertical"
        android:textColor="@android:color/white"
        android:textSize="15sp"
        android:visibility="gone" />

    <Button
        android:id="@+id/btn_validate"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="15dp"
        android:background="@drawable/register_selector"
        android:text="验证"
        android:textColor="@android:color/white"
        android:textSize="18sp" />
</LinearLayout>

设置密保&找回密码逻辑

思路

由于设置密保界面和找回密码界面用的同一个 Activity ,在这个Activity中主要是根据从设置界面和登录界面
putExtra 传递过来的 from 参数的值来判断要跳转到哪个界面,若值为 security 则是设置密保的界面,否则处理的就是找回密码的界面,完成设置密保&找回密码逻辑后再处理设置界面与登录界面的点击事件

具体代码如下:

FindPswActivity

package cn.edu.lt.android.boxueguapp.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.lt.android.boxueguapp.R;
import cn.edu.lt.android.boxueguapp.utils.AnalysisUtils;
import cn.edu.lt.android.boxueguapp.utils.MD5Utils;

public class FindPswActivity extends AppCompatActivity {
    private EditText et_validate_name, et_user_name;
    private Button btn_validate;
    private TextView tv_main_title;
    private TextView tv_back;
    //从哪个界面跳转过来的(from为security时是从设置密保界面跳转过来的,否则就是从登录界面跳转过来的)
    private String from;
    private TextView tv_reset_psw, tv_user_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_psw);
        //设置此界面为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //获取从登录界面和设置界面传递过来的数据
        from = getIntent().getStringExtra("from");
        init();
    }

    /**
     * 获取界面控件及处理相应控件的点击事件
     */
    private void init() {
        tv_main_title = (TextView) findViewById(R.id.tv_main_title);
        tv_back = (TextView) findViewById(R.id.tv_back);
        et_validate_name = (EditText) findViewById(R.id.et_validate_name);
        btn_validate = (Button) findViewById(R.id.btn_validate);
        tv_reset_psw = (TextView) findViewById(R.id.tv_reset_psw);
        et_user_name = (EditText) findViewById(R.id.et_user_name);
        tv_user_name = (TextView) findViewById(R.id.tv_user_name);
        if ("security".equals(from)) {
            tv_main_title.setText("设置密保");
        } else {
            tv_main_title.setText("找回密码");
            //将隐藏的控件显示出来
            tv_user_name.setVisibility(View.VISIBLE);
            et_user_name.setVisibility(View.VISIBLE);
        }
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FindPswActivity.this.finish();
            }
        });
        btn_validate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String validateName = et_validate_name.getText().toString().trim();
                if ("security".equals(from)) {//设置密保
                    if (TextUtils.isEmpty(validateName)) {
                        Toast.makeText(FindPswActivity.this, "请输入要验证的姓名", Toast.LENGTH_SHORT).show();
                        return;
                    } else {
                        Toast.makeText(FindPswActivity.this, "密保设置成功", Toast.LENGTH_SHORT).show();
                        //保存密保到SharedPreferences
                        saveSecurity(validateName);
                        FindPswActivity.this.finish();
                    }
                } else {//找回密码
                    String userName = et_user_name.getText().toString().trim();
                    String sp_security = readSecurity(userName);
                    if (TextUtils.isEmpty(userName)) {
                        Toast.makeText(FindPswActivity.this, "请输入您的用户名", Toast.LENGTH_SHORT).show();
                        return;
                    } else if (!isExistUserName(userName)) {
                        Toast.makeText(FindPswActivity.this, "您输入的用户名不存在", Toast.LENGTH_SHORT).show();
                        return;
                    } else if (TextUtils.isEmpty(validateName)) {
                        Toast.makeText(FindPswActivity.this, "请输入要验证的姓名", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (!validateName.equals(sp_security)) {
                        Toast.makeText(FindPswActivity.this, "输入的密保不正确", Toast.LENGTH_SHORT).show();
                        return;
                    } else {
                        //输入的密保正确,重新给用户设置一个密码
                        tv_reset_psw.setVisibility(View.VISIBLE);
                        tv_reset_psw.setText("初始密码:123456");
                        savePsw(userName);
                    }
                }
            }
        });
    }

    /**
     * 保存初始化密码
     * @param userName
     */
    private void savePsw(String userName) {
        String md5Psw = MD5Utils.md5("123456");//把密码用Md5加密
        SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);//loginInfo表示文件名
        SharedPreferences.Editor editor = sp.edit();//获取编辑器
        editor.putString(userName, md5Psw);
        editor.commit();//提交修改
    }

    /**
     * 保存密保到SharedPreferences中
     */
    private void saveSecurity(String validateName) {
        SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);//loginInfo表示文件名
        SharedPreferences.Editor editor = sp.edit();//获取编辑器
        editor.putString(AnalysisUtils.readLoginUserName(this) + "_security", validateName);//存入账号对应的密保
        editor.commit();//提交修改
    }

    /**
     * 从SharedPreferences中读取密保
     */
    private String readSecurity(String userName) {
        SharedPreferences sp = getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        String security = sp.getString(userName + "_security", "");
        return security;
    }

    /**
     * 从SharedPreferences中根据用户输入的用户名来判断是否有此用户名
     */
    private boolean isExistUserName(String userName) {
        boolean hasUserName = false;
        SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);
        String spPsw = sp.getString(userName, "");
        if (!TextUtils.isEmpty(spPsw)) {
            hasUserName = true;
        }
        return hasUserName;
    }
}

LoginActivity

//找回密码控件的点击事件
        tv_find_psw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转找回密码界面
                Intent intent=new Intent(LoginActivity.this,FindPswActivity.class);
                startActivity(intent);
            }
        });

SettingActivity

//设置密保的点击事件
        rl_security_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SettingActivity.this, FindPswActivity.class);
                intent.putExtra("from", "security");
                startActivity(intent);
            }
        });

优化

思路

为了界面美观以及操作的便捷,找回密码点击验证后,本需要手动回退到登录界面,现本人加入倒计时,倒计时截止时将自动跳转登录界面

FindPswActivity

private int countdown = 3;
//输入的密保正确,重新给用户设置一个密码
                        tv_reset_psw.setVisibility(View.VISIBLE);
                        tv_reset_psw.setTextColor(0xffff0000);
                        tv_reset_psw.setText("初始密码为123456");
                        tv_countdown.setVisibility(View.VISIBLE);
                        tv_countdown.setAlpha(0.5f);
                        tv_countdown.setText("此界面将在"+countdown+"秒后跳转");
                        savePsw(userName);
                        //倒计时跳转
                        final Timer timer = new Timer();
                        TimerTask timerTask = new TimerTask() {
                            @Override
                            public void run() {
                                runOnUiThread(new Runnable() {      // UI thread
                                    @Override
                                    public void run() {
                                        countdown--;
                                        tv_countdown.setVisibility(View.VISIBLE);
                                        tv_countdown.setText("此界面将在"+countdown+"秒后跳转");
                                        if(countdown == 0){
                                            timer.cancel();
                                            Intent intent = new Intent(FindPswActivity.this, LoginActivity.class);
                                            startActivity(intent);
                                            FindPswActivity.this.finish();//跳转后关闭当前页面
                                        }
                                    }
                                });

                            }
                        };

                        timer.schedule(timerTask,1000,1000);

运行效果

《Android项目实战-博学谷》设置密保&找回密码

《Android项目实战-博学谷》设置密保&找回密码

相关推荐