引言
在这半周的工作中,我们小组仍然主要进行后台客户端的代码编写工作以及界面的优化,在这里,我对自己主要从事的工作,即客户端的修改密码功能的实现,做一个重点的介绍。
一、界面编写
在之前的界面设计中,已经完成了该界面的设计工作。根据分析,该界面并不复杂,在最外层使用一个LinearLayout,内部嵌套RelativeLayout或各种组件就可以实现了,具体的代码如下所示:
<?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:background="@color/activity_bg_gray" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/white" android:padding="0dp"> <ImageButton android:id="@+id/back_btn" android:layout_width="?attr/actionBarSize" android:layout_height="?attr/actionBarSize" android:layout_alignParentLeft="true" android:background="@drawable/toolbar_back_bg" android:onClick="modifyPasswordBack" android:src="?attr/homeAsUpIndicator" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="修改密码" android:textColor="@color/black" android:textSize="19sp" /> </RelativeLayout> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/transparent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="0dp" android:background="@color/white" android:gravity="center_horizontal" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/activity_bg_gray" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="40dp" android:text="旧密码:" android:textColor="@color/black" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:orientation="horizontal"> <EditText android:id="@+id/prev_password" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="40dp" android:layout_marginRight="40dp" android:background="@null" android:gravity="center_vertical" android:hint="6-16个字符,区分大小写" android:inputType="textPassword" android:textColor="@color/text_dark_gray" android:textSize="15sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/activity_bg_gray" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="40dp" android:text="新密码:" android:textColor="@color/black" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:orientation="horizontal"> <EditText android:id="@+id/new_password" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="40dp" android:layout_marginRight="40dp" android:background="@null" android:gravity="center_vertical" android:hint="6-16个字符,区分大小写" android:inputType="textPassword" android:textColor="@color/text_dark_gray" android:textSize="15sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/activity_bg_gray" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="40dp" android:text="确认新密码:" android:textColor="@color/black" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:orientation="horizontal"> <EditText android:id="@+id/confirm_new_password" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="40dp" android:layout_marginRight="40dp" android:background="@null" android:gravity="center_vertical" android:hint="6-16个字符,区分大小写" android:inputType="textPassword" android:textColor="@color/text_dark_gray" android:textSize="15sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/activity_bg_gray" android:gravity="center_horizontal" android:orientation="horizontal"> <Button android:layout_width="250dp" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:background="@drawable/login_button_bg" android:text="提交申请" android:onClick="modifyPasswordAction" android:textColor="@color/white" android:textSize="16sp" /> </LinearLayout> </LinearLayout> </ScrollView> </LinearLayout>
根据编写的代码,生成的预览效果如下所示:
可以看到,与预期的结果完全一致
二、内部逻辑实现
在之前的详细设计中,已经完成了这部分功能的设计。这部分的功能也并不复杂,只需要获取用户输入的原密码,新密码,密码重复,并判断长度是否符合要求,原密码与新密码是否相同,两次密码输入是否一致。通过这些检查之后,再次调用NetUtil类的相应方法,与服务器通信,并将返回的结果反馈给用户就可以了。
具体实现代码如下所示:
private boolean getText() { prePass = prev_password.getText().toString(); newPass = new_password.getText().toString(); confirmPass = confirm_new_password.getText().toString(); if (prePass.length() < 6 ||prePass.length()>16 ) { Toast.makeText(this, "原密码长度不符!", Toast.LENGTH_SHORT).show(); return false; } else if ((newPass.length() < 6 ||newPass.length()>16 )) { Toast.makeText(this, "新密码长度不符!", Toast.LENGTH_SHORT).show(); return false; } else if ((confirmPass.length() < 6 ||confirmPass.length()>16 )) { Toast.makeText(this, "确认密码长度不符!", Toast.LENGTH_SHORT).show(); return false; } if (prePass.equals(newPass)) { Toast.makeText(this, "原密码不能和新密码相同!", Toast.LENGTH_SHORT).show(); return false; } else if (!newPass.equals(confirmPass)) { Toast.makeText(this, "两次输入的密码不匹配!", Toast.LENGTH_SHORT).show(); return false; } return true; }
public void modifyPasswordAction(View view) { if (getText()) { ArrayList<NetUtil.Param> params = new ArrayList<>(); String uid = BaseApplication.getInstance().getSharedPreferences("User", MODE_PRIVATE).getString("uid", ""); params.add(new NetUtil.Param("id", uid)); params.add(new NetUtil.Param("newPassword", newPass)); params.add(new NetUtil.Param("oldPassword", prePass)); try { NetUtil.getResult(NetUtil.modifyPassWordUrl, params, new NetUtil.CallBackForResult() { @Override public void onFailure(final IOException e) { ModifyPasswordActivity.this.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(ModifyPasswordActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show(); } }); } @Override public void onSuccess(Response response) { if (response.isSuccessful()) { try { final String res = response.body().string(); ModifyPasswordActivity.this.runOnUiThread(new Runnable() { @Override public void run() { if (res.equals("1")) { Toast.makeText(ModifyPasswordActivity.this, "修改密码成功", Toast.LENGTH_SHORT).show(); ModifyPasswordActivity.this.finish(); } else Toast.makeText(ModifyPasswordActivity.this, "修改密码失败", Toast.LENGTH_SHORT).show(); } }); } catch (IOException e) { e.printStackTrace(); } } } }); } catch (IOException e) { e.printStackTrace(); } } }在编写代码完毕之后,进行了白盒测试,没有发现缺陷,测试通过。