基于eclipse的android项目实战—博学谷(七)修改密码

本项目是用eclipse软件编写,经过我的亲自实践,其真实有效,希望能给您有所帮助
项目版本:android5.1.1
AVD建议:android4.4.2及以上

文章目录

    • 效果图:
    • 1.创建修改密码界面布局文件`activity_modify_pwd.xml`
    • 2.修改密码逻辑代码
    • 3.功能完善

效果图:

1.创建修改密码界面布局文件activity_modify_pwd.xml

res/layout文件夹中新建activity_modify_pwd.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:orientation="vertical" 
    android:background="@drawable/register_bg">
    
    <include layout="@layout/main_title_bar"/>
    
    <EditText 
        android:id="@+id/et_originnal_pwd"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="35dp"
        android:background="@drawable/register_psw"
        android:drawableLeft="@drawable/psw_icon"
        android:drawablePadding="15dp"
        android:gravity="center_vertical"
        android:hint="@string/et_originnal_pwd"
        android:inputType="textPassword"
        android:paddingLeft="10dp"
        android:textColor="#000000"
        android:textColorHint="#a3a3a3"
        android:textSize="20sp"/>
    
    <EditText 
        android:id="@+id/et_new_pwd"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:background="@drawable/register_psw"
        android:drawableLeft="@drawable/psw_icon"
        android:drawablePadding="15dp"
        android:gravity="center_vertical"
        android:hint="@string/et_new_pwd"
        android:inputType="textPassword"
        android:paddingLeft="10dp"
        android:textColor="#000000"
        android:textColorHint="#a3a3a3"
        android:textSize="20sp"/>
    
    <EditText 
        android:id="@+id/et_new_pwd_again"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:background="@drawable/register_psw"
        android:drawableLeft="@drawable/psw_icon"
        android:drawablePadding="15dp"
        android:gravity="center_vertical"
        android:hint="@string/et_new_pwd_again"
        android:inputType="textPassword"
        android:paddingLeft="10dp"
        android:textColor="#000000"
        android:textColorHint="#a3a3a3"
        android:textSize="20sp"/>
    
    <Button 
        android:id="@+id/btn_save"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="15dp"
        android:background="@drawable/register_selector"
        android:text="@string/btn_save"
        android:textColor="@android:color/white"
        android:textSize="22sp"/>

</LinearLayout>

文本信息在values文件夹下的string.xml文件里面:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">BoXueGu</string>
    <string name="hello_world">Hello world!</string>
    <string name="boxuegu">博学谷</string>
    <string name="name">请输入用户名</string>
    <string name="pwd">请输入密码</string>
    <string name="pwd_again">请再次输入密码</string>
    <string name="btn_register">注册</string>
    <string name="login">登录</string>
    <string name="tv_register">立即注册</string>
    <string name="find_pwd">找回密码?</string>
    <string name="course">课程</string>
    <string name="exercises">习题</string>
    <string name="myinfo"></string>
    <string name="tv_user_name">点击登录</string>
    <string name="iv_course_historyicon">播放记录</string>
    <string name="userinfo">设置</string>
    <string name="up_pwd">修改密码</string>
    <string name="bh_pwd">设置密保</string>
    <string name="quit_login">退出登录</string>
    <string name="et_originnal_pwd">请输入原始密码</string>
    <string name="et_new_pwd">请输入新密码</string>
    <string name="et_new_pwd_again">请再次输入新密码</string>
    <string name="btn_save">保存</string>
</resources>

2.修改密码逻辑代码

china.ynyx.heyunhui.activity包中,新建一个Activity类,名为ModifyPswActivity
具体代码:ModifyPswActivity.java

package china.ynyx.heyunhui.activity;

import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
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 china.ynyx.heyunhui.R;
import china.ynyx.heyunhui.utils.AnalysisUtils;
import china.ynyx.heyunhui.utils.MD5Utils;

public class ModifyPswActivity extends AppCompatActivity {

	private TextView tv_main_title,tv_back;
    private EditText et_original_psw,et_new_psw,et_new_psw_again;
    private Button btn_save;
    private String originalPsw,newPsw,newPswAgain,userName;
    
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.activity_modify_pwd);
        //设置此界面为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();
        userName= AnalysisUtils.readLoginUserName(this);
	}

    //获取界面控件并处理相关控件的点击事件
	private void init() {
		// TODO Auto-generated method stub
		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_pwd);
        et_new_psw=(EditText) findViewById(R.id.et_new_pwd);
        et_new_psw_again=(EditText) findViewById(R.id.et_new_pwd_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();
                    //修改登录成功时保存在SharedPreferences中的密码
                    modifyPsw(newPsw);
                    Intent intent = new Intent(ModifyPswActivity.this, LoginActivity.class);
                    startActivity(intent);
                    SettingActivity.instance.finish();
                    ModifyPswActivity.this.finish();
                }
            }
        });
	}

	//获取控件上的字符串
	private void getEditString() {
		// TODO Auto-generated method stub
		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) {
		// TODO Auto-generated method stub
		String md5Psw= MD5Utils.MD5(newPsw);//把密码用MD5加密
        SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();//获取编辑器
        editor.putString(userName, md5Psw);//保存新密码
        editor.commit();//提交修改
	}

	//从SharedPreferences中读取原始密码
	private Object readPsw() {
		// TODO Auto-generated method stub
		SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        String spPsw=sp.getString(userName, "");
        return spPsw;
	}
}

3.功能完善

(1)修改设置界面
由于修改密码界面是通过设置界面跳转的,因此需要在SettingActivity.java文件中的init()方法,在注释“//跳转到修改密码的界面”下方添加如下代码:

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

基于eclipse的android项目实战—博学谷(七)修改密码_第1张图片
(2)修改AndroidManifest.xml清单文件
别忘了在AndroidManifest.xml清单文件中注册:

<activity android:name="china.ynyx.heyunhui.activity.ModifyPswActivity"></activity>

在这里插入图片描述


参考资料:《android项目实战——博学谷》(黑马程序员著)

你可能感兴趣的:(Android)