由于这个模块内容较多,篇幅较长,请耐心阅读。
“我”的模块分为四个部分
一、修改密码
1、创建修改密码界面
在com.boxuegu.activity
包中,创建一个java类,命名为ModifyPswActivity
。在res/layout
文件夹下创建布局文件,命名为activity_modify_psw
。
2、修改密码界面代码——activity_modify_psw.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/register_bg">
<include layout="@layout/main_title_bar"/>
<EditText
android:id="@+id/et_original_psw"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:layout_marginTop="35dp"
android:background="@drawable/register_user_name_bg"
android:drawableLeft="@drawable/psw_icon"
android:drawablePadding="10dp"
android:gravity="center_vertical"
android:hint="请输入原始密码"
android:inputType="textPassword"
android:paddingLeft="8dp"
android:textColor="#000000"
android:textColorHint="#a3a3a3"
android:textSize="14sp"/>
<EditText
android:id="@+id/et_new_psw"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:background="@drawable/register_psw_bg"
android:drawableLeft="@drawable/psw_icon"
android:drawablePadding="10dp"
android:hint="请输入新密码"
android:inputType="textPassword"
android:paddingLeft="8dp"
android:singleLine="true"
android:textColor="#000000"
android:textColorHint="#a3a3a3"
android:textSize="14sp"/>
<EditText
android:id="@+id/et_new_psw_again"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:background="@drawable/register_psw_again_bg"
android:drawableLeft="@drawable/psw_icon"
android:drawablePadding="10dp"
android:hint="请再次输入新密码"
android:inputType="textPassword"
android:paddingLeft="8dp"
android:singleLine="true"
android:textColor="#000000"
android:textColorHint="#a3a3a3"
android:textSize="14sp"/>
<Button
android:id="@+id/btn_save"
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>
3、修改密码界面逻辑代码——ModifyPswActivity.java
package com.boxuegu.activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import com.boxuegu.R;
import com.boxuegu.utils.AnalysisUtils;
import com.boxuegu.utils.MD5Utils;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ModifyPswActivity extends AppCompatActivity {
private TextView tv_main_title;
private TextView tv_back;
private EditText et_original_psw, et_new_psw, et_new_psw_again;
private Button btn_save;
private String originalPsw, newPsw, newPswAgain;
private String userName;
@Override
protected void onCreate(Bundle savedIntanceState){
super.onCreate(savedIntanceState);
//设置界面布局
setContentView(R.layout.activity_modify_psw);
//设置界面为竖屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
init();
userName = AnalysisUtils.readLoginUserName(this);
}
//获取界面控件并处理相关控件的点击事件
private void init(){
tv_main_title = (TextView) findViewById(R.id.tv_main_title);
tv_main_title.setText("修改密码");
tv_back = (TextView) findViewById(R.id.tv_back);
et_original_psw = (EditText) findViewById(R.id.et_original_psw);
et_new_psw = (EditText) findViewById(R.id.et_new_psw);
et_new_psw_again = (EditText) findViewById(R.id.et_new_psw_again);
btn_save = (Button) findViewById(R.id.btn_save);
tv_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ModifyPswActivity.this.finish();
}
});
//保存按钮的点击事件
btn_save.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
getEditString();
if (TextUtils.isEmpty(originalPsw)) {
Toast.makeText(ModifyPswActivity.this, "请输入原始密码",
Toast.LENGTH_SHORT).show();
return;
}else if (!MD5Utils.md5(originalPsw).equals(readPsw())) {
Toast.makeText(ModifyPswActivity.this, "输入的密码与原始密码不一致",
Toast.LENGTH_SHORT).show();
return;
}else if (MD5Utils.md5(newPsw).equals(readPsw())) {
Toast.makeText(ModifyPswActivity.this, "输入的新密码与原始的密码不能一致",
Toast.LENGTH_SHORT).show();
return;
}else if (TextUtils.isEmpty(newPsw)) {
Toast.makeText(ModifyPswActivity.this, "请输入新密码",
Toast.LENGTH_SHORT).show();
return;
}else if (TextUtils.isEmpty(newPswAgain)) {
Toast.makeText(ModifyPswActivity.this, "请再次输入新密码",
Toast.LENGTH_SHORT).show();
return;
}else if (!newPsw.equals(newPswAgain)) {
Toast.makeText(ModifyPswActivity.this, "两次输入的新密码不一致",
Toast.LENGTH_SHORT).show();
return;
} else {
Toast.makeText(ModifyPswActivity.this,"新密码设置成功",
Toast.LENGTH_SHORT).show();
modifyPsw(newPsw);
Intent intent = new Intent(ModifyPswActivity.this, LoginActivity.class);
startActivity(intent);
SettingActivity.instance.finish(); //关闭设置界面
ModifyPswActivity.this.finish(); //关闭本界面
}
}
});
}
//获取控件上的字符串
private void getEditString(){
originalPsw = et_original_psw.getText().toString().trim();
newPsw = et_new_psw.getText().toString().trim();
newPswAgain=et_new_psw_again.getText().toString().trim();
}
//修改登录成功时保存在SharedPreferences中的密码
private void modifyPsw(String newPsw){
String md5Psw = MD5Utils.md5(newPsw);
SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit(); //获取编辑器
editor.putString(userName,md5Psw); //保存新密码
editor.commit(); //提交修改
}
//从SharedPreferences中读取原始密码
private String readPsw(){
SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);
String spPsw = sp.getString(userName, "");
return spPsw;
}
}
4、修改设置界面代码
在设置界面逻辑代码(SettingActivity.java
)文件中找到init
方法,在注释//跳转到修改密码的界面
下方添加如下代码
Intent intent = new Intent(SettingActivity.this,ModifyPswActivity.class);
startActivity(intent);
二、设置密保和找回密码
1、创建设置密保和找回密码界面
在com.boxuegu.activity
包中,创建一个java类,命名为FindPswActivity
。在res/layout
文件夹下创建布局文件,命名为activity_find_psw
。将所需图片find_psw_icon.png
导入drawable
文件夹中。
2、创建设置密保和找回密码界面界面代码——activity_find_psw.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg">
<include layout="@layout/main_title_bar"/>
<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>
3、设置密保和找回密码界面逻辑代码
package com.boxuegu.activity;
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 com.boxuegu.R;
import com.boxuegu.utils.AnalysisUtils;
import com.boxuegu.utils.MD5Utils;
public class FindPswActivity extends AppCompatActivity {
//form为security时从设置密保界面跳转过来,否则就从登录界面跳转过来的
private String from;
private TextView tv_main_title;
private TextView tv_back;
private Button btn_validate;
private EditText et_validate_name;
private TextView tv_reset_psw;
private EditText et_user_name;
private TextView 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 view) {
FindPswActivity.this.finish();
}
});
btn_validate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
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{
//输入的密保正确,重新给用户设置一个密码:123456
tv_reset_psw.setVisibility(View.VISIBLE);
tv_reset_psw.setText("初始密码:123456");
savePsw(userName);
}
}
}
});
}
//保存初始化的密码
private void savePsw(String userName){
String md5Psw = MD5Utils.md5("123456"); //密码MD5加密
SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit(); //获取编辑器
editor.putString(userName,md5Psw);
editor.commit(); //提交修改
}
private void saveSecurity(String validateName){
SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit(); //获取编辑器
//存入对应账户的密保
editor.putString(AnalysisUtils.readLoginUserName(this)+"_security",validateName);
editor.commit(); //提交修改
}
//从SharedPreferences中读取密保
private String readSecurity(String userName){
SharedPreferences sp = getSharedPreferences("loginInfo",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;
}
}
4、修改代码
(1)、修改登录界面
到登录界面逻辑代码(LoginActivity.java
)文件夹中找到init
方法。在注释//跳转到找回密码界面
下方添加如下代码
Intent intent = new Intent(LoginActivity.this, FindPswActivity.class);
startActivity(intent);
(2)、修改设置界面
到设置界面逻辑代码(SettingActivity.java
)文件夹中找到init
方法。在注释//跳转到设置密保界面
下方添加如下代码
Intent intent = new Intent(SettingActivity.this,FindPswActivity.class);
intent.putExtra("from","security");
startActivity(intent);
Android项目实战系列—基于博学谷 开源地址
来源:oschina
链接:https://my.oschina.net/cztblog/blog/4260661