引言
在这半周的工作中,我们小组仍然主要进行后台客户端的代码编写工作以及界面的优化,在这里,我对自己主要从事的工作,即客户端的查看个人信息的实现,做一个重点的介绍。
查看个人信息
1、界面编写
查看个人信息的界面图如下:
xml代码十分简单,首先顶部titlebar是一个RelativeLayout,有一个返回按钮,一个textview和一个编辑按钮,分别设置属性控制他们的位置。
然后下面是三个LinearLayout,具体代码如下:
<?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:gravity="center_horizontal"
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="lookInfoBackTo"
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" />
<Button
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentRight="true"
android:background="@color/transparent"
android:onClick="editInfo"
android:text="编辑"
android:textColor="@color/text_blue"
android:textSize="18sp" />
</RelativeLayout>
<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" />
<TextView
android:id="@+id/nick_name_tv"
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" />
<TextView
android:id="@+id/sex_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:background="@null"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:textColor="@color/text_dark_gray"
android:textSize="17sp" />
</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="16sp" />
<TextView
android:id="@+id/info_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:background="@null"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:textColor="@color/text_dark_gray"
android:textSize="17sp" />
</LinearLayout>
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_gravity="center_horizontal" />
</LinearLayout>
2、java逻辑编写
查看个人信息这里不需要向服务器请求数据,因为要显示的昵称,性别以及个性签名在用户登录后都会存储到本地,所以直接从本地获取数据即可。当点击“编辑”按钮的时候,需要跳转到编辑界面即可,所以代码逻辑十分简单。具体代码如下:
/**
* 查看个人信息
*/
public class LookInfoActivity extends BaseActivity {
private TextView nickNameTv, infoTv, sex_tv;
private String nickname, sex, info;
private boolean isEdit;
private int select = BaseApplication.getInstance().getSharedPreferences("User", Context.MODE_PRIVATE).getInt("photo", 0);
@Override
public int getLayoutId() {
return R.layout.activity_lookinfo;
}
@Override
public void afterCreate(Bundle savedInstanceState) {
nickNameTv = (TextView) findViewById(R.id.nick_name_tv);
infoTv = (TextView) findViewById(R.id.info_tv);
sex_tv = (TextView) findViewById(R.id.sex_tv);
Intent intent = getIntent();
nickname = intent.getStringExtra("nickname");
sex = intent.getStringExtra("sex");
info = intent.getStringExtra("info");
isEdit = false;
sex_tv.setText(sex);
nickNameTv.setText(nickname);
infoTv.setText(info);
}
public void editInfo(View view) {
Intent intent = new Intent(LookInfoActivity.this, EditInfoActivity.class);
intent.putExtra("nickname", nickNameTv.getText());
intent.putExtra("info", infoTv.getText());
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) {
case 1:
String nicknameStr = data.getStringExtra("nickname");
String infoStr = data.getStringExtra("information");
select = data.getIntExtra("photo", 0);
nickNameTv.setText(nicknameStr);
infoTv.setText(infoStr);
nickname = nicknameStr;
info = infoStr;
isEdit = true;
break;
}
}
@Override
public void onBackPressed() {
if (isEdit) {
Intent intent = new Intent();
intent.putExtra("nickname", nickname);
intent.putExtra("info", info);
intent.putExtra("photo", select);
setResult(1, intent);
LookInfoActivity.this.finish();
} else
LookInfoActivity.this.finish();
}
public void lookInfoBackTo(View view) {
onBackPressed();
}
}
3、测试
在代码完成后,我们进行了简单的测试,使用白盒测试的方法设计了一些用例,全部通过后才完成工作。