十天冲刺之九

掌握修改密码功能的开发,实现用户密码的修改;
掌握设置密保功能的开发,并且通过密保可以找回用户密码。
这是今天学习的主要内容:
首先就是布局的问题
<?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="@android:color/white"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_head"
android:layout_width="match_parent"
android:layout_height="240dp"
android:background="@drawable/myinfo_login_bg"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_head_icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="75dp"
android:src="@drawable/default_icon" />
<TextView
android:id="@+id/tv_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="点击登录"
android:textColor="@android:color/white"
android:textSize="16sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="20dp"
android:background="#E3E3E3" />
<RelativeLayout
android:id="@+id/rl_course_history"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#F7F8F8"
android:gravity="center_vertical">
<ImageView
android:id="@+id/iv_course_history_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_marginLeft="25dp"
android:src="@drawable/course_history_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="25dp"
android:layout_toRightOf="@id/iv_course_history_icon"
android:text="播放记录"
android:textColor="#A3A3A3"
android:textSize="16sp" />
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="25dp"
android:src="@drawable/iv_right_arrow" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#E3E3E3" />
<RelativeLayout
android:id="@+id/rl_setting"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#F7F8F8"
android:gravity="center_vertical">
<ImageView
android:id="@+id/iv_userInfo_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_marginLeft="25dp"
android:src="@drawable/myinfo_setting_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="25dp"
android:layout_toRightOf="@id/iv_userInfo_icon"
android:text="设置"
android:textColor="#A3A3A3"
android:textSize="16sp" />
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="25dp"
android:src="@drawable/iv_right_arrow" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#E3E3E3" />
</LinearLayout>
“我”的界面能显示后,我们来写个工具类。
由于项目多次用到sharedPreferences共享参数去存储用户的登录状态或清除登录状态,“我”的界面也要求用到读取用户姓名的方法,所以我们干脆把这三个方法都扔到AnalysisUtils里面吧。
在Utils包中新建一个Java类,名为AnalysisUtils。
import android.content.Context;
import android.content.SharedPreferences;
public class AnalysisUtils {
//读取用户名
public static String readLoginUserName(Context context){
SharedPreferences sharedPreferences = context.getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
String userName=sharedPreferences.getString("loginUserName","");
return userName;
}
//读取登录状态
public static boolean readLoginStatus(Context context){
SharedPreferences sharedPreferences = context.getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
boolean isLogin=sharedPreferences.getBoolean("isLogin",false);
return isLogin;
}
//清除登录状态
public static void cleanLoginStatus(Context context){
SharedPreferences sharedPreferences = context.getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("isLogin",false);
editor.putString("loginUserName","");
editor.commit();
}
}
加上View.OnClickListener接口
public class MyinfoFragment extends Fragment implements View.OnClickListener{
以及监听器功能
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
llHead = (LinearLayout) view.findViewById(R.id.ll_head);
ivHeadIcon = (ImageView) view.findViewById(R.id.iv_head_icon);
tvUserName = (TextView) view.findViewById(R.id.tv_user_name);
rlCourseHistory = (RelativeLayout) view.findViewById(R.id.rl_course_history);
ivCourseHistoryIcon = (ImageView) view.findViewById(R.id.iv_course_history_icon);
rlSetting = (RelativeLayout) view.findViewById(R.id.rl_setting);
ivUserInfoIcon = (ImageView) view.findViewById(R.id.iv_userInfo_icon);
llHead.setOnClickListener(this);
rlCourseHistory.setOnClickListener(this);
rlSetting.setOnClickListener(this);
}
在我”的界面的头像部分,要实现两个功能
- 打开“我”的界面后要判断是否已登录,已登录显示用户名,未登录显示“点击登陆”。
- 头像部分点击后,会判断是否登录,如果登录了,则跳转到个人资料界面,如果没登录,则跳转到login页面。
先来完成第一个功能@Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); llHead = (LinearLayout) view.findViewById(R.id.ll_head); ivHeadIcon = (ImageView) view.findViewById(R.id.iv_head_icon); tvUserName = (TextView) view.findViewById(R.id.tv_user_name); rlCourseHistory = (RelativeLayout) view.findViewById(R.id.rl_course_history); ivCourseHistoryIcon = (ImageView) view.findViewById(R.id.iv_course_history_icon); rlSetting = (RelativeLayout) view.findViewById(R.id.rl_setting); ivUserInfoIcon = (ImageView) view.findViewById(R.id.iv_userInfo_icon); if (AnalysisUtils.readLoginStatus(getActivity())){ tvUserName.setText(AnalysisUtils.readLoginUserName(getActivity())); }else { tvUserName.setText("点击登录"); } llHead.setOnClickListener(this); rlCourseHistory.setOnClickListener(this); rlSetting.setOnClickListener(this); }通过if()else语句进行判断,根据当前登录状态来显示同像下相应的文本。判断的内容就可以调用我们刚才写过的工具类啦。然后在onClick()方法里给头像部分的点击以及其他按钮加上判断
@Override public void onClick(View v) { switch (v.getId()){ case R.id.ll_head: if (AnalysisUtils.readLoginStatus(getActivity())){ //跳转到个人资料界面 }else { //跳转到登录界面 Intent intent = new Intent(getActivity(), LoginActivity.class); getActivity().startActivityForResult(intent,1); } break; case R.id.rl_course_history: if (AnalysisUtils.readLoginStatus(getActivity())){ //跳转到播放记录页面 }else { Toast.makeText(getActivity(),"您未登录,请先登录",Toast.LENGTH_SHORT).show(); } break; case R.id.rl_setting: if (AnalysisUtils.readLoginStatus(getActivity())){ //跳转到设置界面 }else { Toast.makeText(getActivity(),"您未登录,请先登录",Toast.LENGTH_SHORT).show(); } break; } }

浙公网安备 33010602011771号