本项目是用eclipse软件编写,经过我的亲自实践,其真实有效,希望能给您有所帮助😘😘

项目版本:android5.1.1

AVD建议:android4.4.2及以上



BoXueGu图片资源下载(免费):
https://yuyunyaohui.lanzoui.com/iWos0pyc4rc


效果图:

android 密码界面修改密码 安卓修改密码流程_找回密码

android 密码界面修改密码 安卓修改密码流程_android 密码界面修改密码_02

1.创建设置密保和找回密码界面布局文件activity_find_psw.xml

res/layout文件夹中新建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:orientation="vertical"
    android:background="@drawable/register_bg" >
    
    <include layout="@layout/main_title_bar"/>
    
    <TextView 
        android:id="@+id/tv_user_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="35dp"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="35dp"
        android:text="@string/TV_user_name"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:visibility="gone"/>
    
    <EditText 
        android:id="@+id/et_user_name"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_marginRight="35dp"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/register_psw"
        android:hint="@string/et_user_name"
        android:paddingLeft="10dp"
        android:singleLine="true"
        android:textColor="#000000"
        android:textColorHint="#a3a3a3"
        android:visibility="gone"/>
    
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="35dp"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="15dp"
        android:text="@string/your_name"
        android:textColor="@android:color/white"
        android:textSize="20sp"/>
    
    <EditText 
        android:id="@+id/et_validate_name"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_marginRight="35dp"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/register_psw"
        android:hint="@string/et_validate_name"
        android:paddingLeft="10dp"
        android:singleLine="true"
        android:textColor="#000000"
        android:textColorHint="#a3a3a3"/>
    
    <TextView 
        android:id="@+id/tv_reset_psw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="35dp"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_vertical"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        android:visibility="gone"/>
    
    <Button 
        android:id="@+id/btn_validate"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginRight="35dp"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="15dp"
        android:background="@drawable/register_selector"
        android:text="@string/btn_validate"
        android:textColor="@android:color/white"
        android:textSize="20sp"/>
</LinearLayout>

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

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="TV_user_name">您的用户名是?</string>
    <string name="et_user_name">请输入你的用户名</string>
    <string name="your_name">您的姓名是?</string>
    <string name="et_validate_name">请输入要验证的姓名</string>
    <string name="btn_validate">验证</string>
</resources>

2.设置密保和找回密码界面逻辑代码

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

package china.ynyx.heyunhui.activity;

import android.support.v7.app.AppCompatActivity;
import android.content.Context;
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 FindPswActivity extends AppCompatActivity {

	private TextView tv_main_title,tv_back,tv_user_name,tv_reset_psw;
    private Button btn_validate;
    private EditText et_user_name,et_validate_name;
    //from为security是从设置密保界面跳转过来的,否则就是从登录界面跳转过来的
    private String from;
    
    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() {
		// 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_validate_name=(EditText)findViewById(R.id.et_validate_name);
        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);
        btn_validate=(Button)findViewById(R.id.btn_validate);
        //通过from判断当前界面
        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);
                    }
                }
            }
        });
	}

	//保存初始化密码
	private void savePsw(String userName) {
		// TODO Auto-generated method stub
		String md5Psw = MD5Utils.MD5("123456");
        SharedPreferences sp = getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor =sp.edit();//获取编辑器
        editor.putString(userName,md5Psw);       //保存新密码
        editor.commit();
	}

	//保存密保到SharedPreferences中
	private void saveSecurity(String validateName) {
		// TODO Auto-generated method stub
		SharedPreferences sp = getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor =sp.edit();  //获取编辑器
        editor.putString(AnalysisUtils.readLoginUserName(this)+"_security",validateName);       //保存新密码
        editor.commit();
	}

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

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

3.功能完善

(1)修改登录界面
由于找回密码界面是通过登录界面跳转的,因此需要在LoginActivity.java文件中的init()方法,在注释//跳转到找回密码界面(此界面暂时未创建)下方添加如下代码:

Intent intent=new Intent(LoginActivity.this,FindPswActivity.class);
				startActivityForResult(intent, 1);

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

Intent intent = new Intent(SettingActivity.this,FindPswActivity.class);
                intent.putExtra("from","security");
                startActivity(intent);

(3)修改AndroidManifest.xml清单文件
同样的,别忘了在清单文件中注册:

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

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