1.实现修改密码界面,主要是让用户在必要时修改自己的原始密码,从而保证用户信息的安全性。
2.界面效果如下:效果如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_modify_psw"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/register_bg"
tools:context="itcast.cn.boxuegu.activity.ModifyPswActivity">
<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_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_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_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"
/>
<Button
android:id="@+id/btn_save"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:layout_marginTop="15dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/register_selector"
android:text="保存"
android:textColor="@android:color/white"
android:textSize="18sp"
/>
</LinearLayout>
3.在实现修改密码界面逻辑代码中,主要用于输入原始密码,新密码,再次修改新密码。输入的原始密码与从SharedPrefenences中读取的原始密码必须一致,输入的新密码与原始密码不能相同,再次输入的新密码与与输入的新密码必须相同。以上条件都符合之后,点击“保存”按钮提示设置成功,同时修改Sharedference中的原始密码。
package itcast.cn.boxuegu.activity;
import android.content.Intent;
import android.content.SharedPreferences;
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 itcast.cn.boxuegu.R;
import itcast.cn.boxuegu.utils.AnalysisUtils;
import itcast.cn.boxuegu.utils.MD5Utils;
public class ModifyPswActivity extends AppCompatActivity {
private TextView tv_main_title;
private TextView tv_back;
private EditText et_original_psw;
private EditText et_new_psw;
private EditText et_new_psw_again;
private Button btn_save;
private String originalPsw;
private String newPsw;
private String newPswAgain;
private String userName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_modify_psw);
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);
tv_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ModifyPswActivity.this.finish();//关闭当前界面
}
});
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);
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();
}else if (!MD5Utils.md5(originalPsw).equals(readPsw())){
Toast.makeText(ModifyPswActivity.this,"输入的密码与原始密码不一致",Toast.LENGTH_SHORT).show();
}else if (MD5Utils.md5(newPsw).equals(readPsw())){
Toast.makeText(ModifyPswActivity.this,"输入的新密码与原始密码不能一致",Toast.LENGTH_SHORT).show();
}else if (TextUtils.isEmpty(newPsw)){
Toast.makeText(ModifyPswActivity.this,"请输入新密码",Toast.LENGTH_SHORT).show();
}else if (TextUtils.isEmpty(newPswAgain)){
Toast.makeText(ModifyPswActivity.this,"请再次输入新密码",Toast.LENGTH_SHORT).show();
}else if (!newPsw.equals(newPswAgain)){
Toast.makeText(ModifyPswActivity.this,"两次输入的新密码不一致",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(ModifyPswActivity.this,"新密码设置成功",Toast.LENGTH_SHORT).show();
//修改登录成功后保存在SharedPreferences中的密码
modifyPsw(newPsw);
Intent intent = new Intent(ModifyPswActivity.this,LoginActivity.class);
startActivity(intent);
ModifyPswActivity.this.finish();//关闭当前界面
SettingActivity.instance.finish();//关闭设置界面
}
}
});
}
//修改登录成功后保存在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;
}
//h获取控件上的字符串
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();
}
}