Android项目实战系列—基于博学谷(四)我的模块(下)

Stella981
• 阅读 576

Android项目实战系列—基于博学谷(四)我的模块(下)

由于这个模块内容较多,篇幅较长,请耐心阅读。


“我”的模块分为四个部分

  • [ ] 我的界面
  • [ ] 设置界面
  • [x] 修改密码界面
  • [x] 设置密保和找回密码

一、修改密码

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项目实战系列—基于博学谷 开源地址

Android项目实战系列—基于博学谷(四)我的模块(下)                 Android项目实战系列—基于博学谷(四)我的模块(下)

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Stella981 Stella981
2年前
Android项目实战系列—基于博学谷(七)课程模块(下)
!image(https://www.cztcms.cn/wpcontent/uploads/2020/03/%E5%8D%9A%E5%AD%A6%E8%B0%B7.png)由于这个模块内容较多,分为上、中、下三篇博客分别来讲述,请耐心阅读。课程模块分为四个部分\\课程列表\\课
Stella981 Stella981
2年前
Android项目实战系列—基于博学谷(一)项目综述
!image(https://www.cztcms.cn/wpcontent/uploads/2020/03/%E5%8D%9A%E5%AD%A6%E8%B0%B7.png)一、项目分析1、项目名称WordPress建站APP2、项目概述WordPress建站是一个学习博客建站技术的APP
Stella981 Stella981
2年前
Android项目实战系列—基于博学谷(四)我的模块(上)
!image(https://www.cztcms.cn/wpcontent/uploads/2020/03/%E5%8D%9A%E5%AD%A6%E8%B0%B7.png)由于这个模块内容较多,篇幅较长,请耐心阅读。“我”的模块分为四个部分\x\我的界面\x\设置界面\
Stella981 Stella981
2年前
Android项目实战系列—基于博学谷(三)注册与登录模块
!image(https://www.cztcms.cn/wpcontent/uploads/2020/03/%E5%8D%9A%E5%AD%A6%E8%B0%B7.png)由于这个模块内容较多,篇幅较长,请耐心阅读。注册与登录模块分为三个部分\x\欢迎界面\x\注册界面\
Stella981 Stella981
2年前
Android项目实战系列—基于博学谷(七)课程模块(中)
!image(https://www.cztcms.cn/wpcontent/uploads/2020/03/%E5%8D%9A%E5%AD%A6%E8%B0%B7.png)由于这个模块内容较多,分为上、中、下三篇博客分别来讲述,请耐心阅读。课程模块分为四个部分\\课程列表\x\课
Stella981 Stella981
2年前
Android项目实战系列—基于博学谷(六)习题模块
!image(https://www.cztcms.cn/wpcontent/uploads/2020/03/%E5%8D%9A%E5%AD%A6%E8%B0%B7.png)由于这个模块内容较多,篇幅较长,请耐心阅读。习题模块分为两个部分\x\习题列表界面\x\习题详情界面
Stella981 Stella981
2年前
Android项目实战系列—基于博学谷(七)课程模块(上)
!image(https://www.cztcms.cn/wpcontent/uploads/2020/03/%E5%8D%9A%E5%AD%A6%E8%B0%B7.png)由于这个模块内容较多,分为上、中、下三篇博客分别来讲述,请耐心阅读。课程模块分为四个部分\x\课程列表\\课
Stella981 Stella981
2年前
Android项目实战系列—基于博学谷(五)个人资料
!image(https://www.cztcms.cn/wpcontent/uploads/2020/03/%E5%8D%9A%E5%AD%A6%E8%B0%B7.png)由于这个模块内容较多,篇幅较长,请耐心阅读。个人资料模块分为两个部分\x\个人资料\x\资料修改
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这