引言
本周我们客户端继续进行代码编写的工作,前两天的时间里,我的工作是进行“编辑个人信息”功能的代码编写,在这里做一下介绍。
编辑个人信息
1、界面设计
界面设计中,编辑个人信息界面可以由查看个人信息界面跳转,在查看个人信息界面的顶部bar的右侧有一个编辑按钮,点击编辑即可跳转到编辑个人信息界面,可以修改的信息有头像、昵称、性别、个人签名。下面是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:background="@color/activity_bg_gray"
android:orientation="vertical"
android:gravity="center_horizontal">
<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="editInfoBackTo"
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>
<ImageView
android:id="@+id/head_icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:scaleType="fitCenter"
android:src="@drawable/head_icon_default"
android:layout_marginTop="12dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改头像"
android:textSize="17sp"
android:layout_marginTop="12dp"
android:textColor="@color/login_button_default_blue"
android:onClick="editHeadIcon"
android:background="@null"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="20dp"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:text="昵称"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/nick_name_edt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:background="@null"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="陌年之夏"
android:textColor="@color/text_dark_gray"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="2dp"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:text="性别"
android:textColor="@color/black"
android:textSize="17sp" />
<carbon.widget.RadioGroup
android:id="@+id/sex_radio_group"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:paddingLeft="10dp"
android:orientation="horizontal">
<carbon.widget.RadioButton
android:id="@+id/male_rb"
android:layout_width="wrap_content"
android:layout_height="@dimen/carbon_listItemHeight"
android:gravity="center_vertical"
android:text="男"
android:checked="true"/>
<carbon.widget.RadioButton
android:id="@+id/female_rb"
android:layout_width="wrap_content"
android:layout_height="@dimen/carbon_listItemHeight"
android:gravity="center_vertical"
android:text="女" />
</carbon.widget.RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="20dp"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:text="个性签名"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/signature_edt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:background="@null"
android:gravity="center_vertical"
android:hint="介绍下自己吧"
android:paddingLeft="10dp"
android:textColor="@color/text_dark_gray"
android:textColorHint="@color/text_hint_gray"
android:textSize="17sp" />
</LinearLayout>
<Button
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="提交"
android:textSize="16sp"
android:textColor="@color/white"
android:padding="8dp"
android:layout_marginTop="50dp"
android:background="@drawable/login_button_bg"
android:onClick="editInfoPost"/>
</LinearLayout>
编写出的界面如图所示:
点击修改头像按钮,可以进入头像修改界面,修改头像界面编写后如图:
可供选择的头像在本地存储,在服务器端存储头像编号即可,具体的代码这里不再详述。
2、逻辑代码设计
代码逻辑设计流程如下:
- 判断网络是否可用
- 若可用,当点击“提交”按钮,判断头像是否修改
- 若修改则添加头像参数,然后判断昵称是否为空
- 昵称为空则返回错误,不为空则判断性别是否为空
- 性别为空则返回错误,不为空则判断个性签名是否为空
- 个性签名允许为空,若为空则不添加个签参数,不为空则添加
- 将所有参数即值通过网络请求发送出去,等待服务器响应
- 根据不用的响应,进行不同的操作
网络请求回掉函数如下:
class EditInfoCallBack implements NetUtil.CallBackForResult {
@Override
public void onFailure(final IOException e) {
EditInfoActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(EditInfoActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onSuccess(Response response) {
if (response.isSuccessful()) {
try {
String res = response.body().string();
if (res.equals("1")) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("nickname", nickName).apply();
editor.putString("information", signature).apply();
editor.putString("sex", sex).apply();
editor.putInt("photo", select).apply();
EditInfoActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(EditInfoActivity.this, "修改信息成功", Toast.LENGTH_LONG).show();
Intent intent = new Intent();
intent.putExtra("nickname", nickName);
intent.putExtra("information", signature);
intent.putExtra("sex", sex);
intent.putExtra("photo", select);
EditInfoActivity.this.setResult(1, intent);
EditInfoActivity.this.finish();
}
});
} else if (res.equals("0")) {
EditInfoActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(EditInfoActivity.this, "修改信息失败", Toast.LENGTH_LONG).show();
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3、代码测试
在代码编写完毕后,我和朱宏进行了代码互查和互测,我们采用白盒测试技术中的基本路径覆盖设计测试用例。针对不同的输入信息,设计了多组测试数据,然后再真机上对每一组数据进行实际测试,检查后台返回数据是否,直到双方编写的界面测试运行无误。
总结
这两天的工作在界面上还有一定的欠缺美观,在随后的工作中,我会对这两天的界面再重新整改。