侧边栏壁纸
博主头像
微笑涛声

一个在编程学习中逐渐变强的孩子! 热爱编程,喜欢软件开发!

  • 累计撰写 129 篇文章
  • 累计创建 58 个标签
  • 累计收到 2 条评论
隐藏侧边栏

Android项目实战系列—基于博学谷(四)我的模块(上)

微笑涛声
2020-04-30 / 0 评论 / 0 点赞 / 95 阅读 / 17,608 字
温馨提示:
本文最后更新于 2021-06-16,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

image

由于这个模块内容较多,篇幅较长,请耐心阅读。


“我”的模块分为四个部分


一、“我”的界面

1、底部导航栏

(1)、导入界面图片

将底部导航栏所需图片main_course_icon.pngmain_course_icon_selected.pngmain_exercises_icon.pngmain_exercises_icon_selected.pngmain_my_icon.pngmain_my_icon_selected.png导入到drawable文件夹中。

(2)、底部导航栏界面代码——activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"
        android:background="@android:color/white"
        android:orientation="vertical">
        
        <!-- 导入标题栏——main_title_bar.xml文件 -->
        <include layout="@layout/main_title_bar"/>
        
        <!-- 主界面背景 -->
        <FrameLayout
            android:id="@+id/main_body"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"/>
    </LinearLayout>
    
    <!-- 底部导航栏界面设置 -->
    <LinearLayout
        android:id="@+id/main_bottom_bar"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_alignParentBottom="true"
        android:background="#F2F2F2"
        android:orientation="horizontal">
        
        	<!-- 底部导航栏课程 -->
        <RelativeLayout
            android:id="@+id/bottom_bar_course_btn"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1">
            <TextView
                android:id="@+id/bottom_bar_text_course"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="3dp"
                android:gravity="center"
                android:singleLine="true"
                android:text="课程"
                android:textColor="#666666"
                android:textSize="14sp"/>
            <ImageView
                android:id="@+id/bottom_bar_image_course"
                android:layout_width="27dp"
                android:layout_height="27dp"
                android:layout_above="@id/bottom_bar_text_course"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="3dp"
                android:src="@drawable/main_course_icon"/>
        </RelativeLayout>
        
        <!-- 底部导航栏习题 -->
        <RelativeLayout
            android:id="@+id/bottom_bar_exercises_btn"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1">
            <TextView
                android:id="@+id/bottom_bar_text_exercises"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="3dp"
                android:gravity="center"
                android:singleLine="true"
                android:text="习题"
                android:textColor="#666666"
                android:textSize="14sp"/>
            <ImageView
                android:id="@+id/bottom_bar_image_exercises"
                android:layout_width="27dp"
                android:layout_height="27dp"
                android:layout_above="@id/bottom_bar_text_exercises"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="3dp"
                android:src="@drawable/main_exercises_icon"/>
        </RelativeLayout>
        
        <!-- 底部导航栏我 -->
        <RelativeLayout
            android:id="@+id/bottom_bar_myinfo_btn"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1">
            <TextView
                android:id="@+id/bottom_bar_text_myinfo"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="3dp"
                android:gravity="center"
                android:singleLine="true"
                android:text="我"
                android:textColor="#666666"
                android:textSize="14sp"/>
            <ImageView
                android:id="@+id/bottom_bar_image_myinfo"
                android:layout_width="27dp"
                android:layout_height="27dp"
                android:layout_above="@id/bottom_bar_text_myinfo"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="3dp"
                android:src="@drawable/main_my_icon"/>
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

(3)、底部导航栏逻辑代码——MainActivity.java

package com.boxuegu.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com. boxuegu. R;
import com.boxuegu.view.MyInfoView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

	//中间内容栏
    private FrameLayout mBodyLayout;
    //底部导航栏按钮
    private LinearLayout mBottomLayout;

    //底部按钮
    private View mCourseBtn;
    private View mExercisesBtn;
    private View mMyInfoBtn;
    private TextView tv_course;
    private TextView tv_exercises;
    private TextView tv_myInfo;
    private ImageView iv_course;
    private ImageView iv_exercises;
    private ImageView iv_myInfo;
    private TextView tv_back;
    private TextView tv_main_title;
    private RelativeLayout rl_title_bar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置界面布局
        setContentView(R.layout.activity_main);
        //设置界面为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();
        initBottomBar();
        setListener();
        setInitStatus();
    }
    
	/* 
	 * 获取界面上的UI控件
	 */
    private void init() {
        tv_back = (TextView)findViewById(R.id.tv_back);
        tv_main_title = (TextView) findViewById(R.id.tv_main_title);
        tv_main_title.setText("WordPress课程");
        rl_title_bar = (RelativeLayout)findViewById(R.id.title_bar);
        rl_title_bar.setBackgroundColor(Color.parseColor("#FF9900"));
        tv_back.setVisibility(View.GONE);
        initBodyLayout();
    }
    
    /* 
	 * 获取底部导航栏上的控件
	 */
    private void initBottomBar() {
        mBottomLayout = (LinearLayout) findViewById(R.id.main_bottom_bar);
        mCourseBtn=findViewById(R.id.bottom_bar_course_btn);
        mExercisesBtn= findViewById(R.id.bottom_bar_exercises_btn);
        mMyInfoBtn = findViewById(R.id.bottom_bar_myinfo_btn);
        tv_course=(TextView)findViewById(R.id.bottom_bar_text_course);
        tv_exercises=(TextView)findViewById(R.id.bottom_bar_text_exercises);
        tv_myInfo = (TextView)findViewById(R.id.bottom_bar_text_myinfo);
        iv_course =(ImageView)findViewById(R.id.bottom_bar_image_course);
        iv_exercises= (ImageView)findViewById(R.id.bottom_bar_image_exercises);
        iv_myInfo= (ImageView)findViewById(R.id.bottom_bar_image_myinfo);

    }
    private void initBodyLayout() {
        mBodyLayout = (FrameLayout) findViewById(R.id.main_body);
    }

    /* 
	 * 控件的点击事件
	 */
    @Override
    public void onClick(View v){
        switch (v.getId()){
        	//课程的点击事件
            case R.id.bottom_bar_course_btn:
                clearBottomImageState();
                selectDisplayView(0);
                break;
                
            //习题的点击事件
            case R.id.bottom_bar_exercises_btn:
                clearBottomImageState();
                selectDisplayView(1);
                break;
                
             //我的点击事件
            case R.id.bottom_bar_myinfo_btn:
                clearBottomImageState();
                selectDisplayView(2);
                break;
            default:
                break;
        }
    }
    
    /* 
	 * 设置底部三个按钮的点击监听事件
	 */
    private void setListener() {
        for (int i = 0; i < mBottomLayout.getChildCount();i++){
            mBottomLayout.getChildAt(i).setOnClickListener(this);
        }
    }

    /* 
	 * 清除底部按钮的选中状态
	 */
    private void clearBottomImageState() {
        tv_course.setTextColor(Color.parseColor("#666666"));
        tv_exercises.setTextColor(Color.parseColor("#666666"));
        tv_myInfo.setTextColor(Color.parseColor("#666666"));
        iv_course.setImageResource(R.drawable.main_course_icon);
        iv_exercises.setImageResource(R.drawable.main_exercises_icon);
        iv_myInfo.setImageResource(R.drawable.main_my_icon);
        for (int i=0;i<mBottomLayout.getChildCount();i++){
            mBottomLayout.getChildAt(i).setSelected(false);
        }
    }

    /* 
	 * 设置底部按钮的选中状态
	 */
    public void setSelectedStatus(int index){
        switch (index){
            case 0:
                mCourseBtn.setSelected(true);
                iv_course.setImageResource(R.drawable.main_course_icon_selected);
                tv_course.setTextColor(Color.parseColor("#0097F7"));
                rl_title_bar.setVisibility(View.VISIBLE);
                tv_main_title.setText("WordPress课程");
                break;
            case 1:
                mExercisesBtn.setSelected(true);
                iv_exercises.setImageResource(R.drawable.main_exercises_icon_selected);
                tv_exercises.setTextColor(Color.parseColor("#0097F7"));
                rl_title_bar.setVisibility(View.VISIBLE);
                tv_main_title.setText("WordPress习题");
                break;
            case 2:
                mMyInfoBtn.setSelected(true);
                iv_myInfo.setImageResource(R.drawable.main_my_icon_selected);
                tv_myInfo.setTextColor(Color.parseColor("#0097F7"));
                rl_title_bar.setVisibility(View.GONE);
        }
    }
    
    /* 
	 * 移除不需要的视图
	 */
    private void removeAllView(){
        for (int i = 0;i<mBodyLayout.getChildCount();i++){
            mBodyLayout.getChildAt(i).setVisibility(View.GONE);
        }
    }

    /* 
	 * 设置界面view的初始化状态
	 */
    private void setInitStatus() {
        clearBottomImageState();
        setSelectedStatus(0);
        createView(0);

    }

    /* 
	 * 显示对应的页面
	 */
    private void selectDisplayView(int index) {
        removeAllView();
        createView(index);
        setSelectedStatus(index);
    }

    /* 
	 * 选中视图
	 */
    private void createView(int viewIndex) {
        switch (viewIndex){
            case 0:
                //课程界面
                break;
            case 1:
               //习题界面
                break;
            case 2:
               //我的界面
                break;
        }
    }

    protected long exitTime; //记录第一次点击的时间

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if ((System.currentTimeMillis()-exitTime)>2000){
                Toast.makeText(MainActivity.this,"再按一次退出WordPress",Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            }else{
                MainActivity.this.finish();
                if (readLoginStatus()){
                    clearLoginStatus();
                }
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);

    }

    //获取SharedPreferences中的登录状态
    private boolean readLoginStatus() {
        SharedPreferences sp = getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        boolean isLogin = sp.getBoolean("isLogin",false);
        return isLogin;
    }

    //清除SharedPreferences中的登录状态
    private void clearLoginStatus() {
        SharedPreferences sp = getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();  //读取编辑器
        editor.putBoolean("isLogin",false);   //清除登录状态
        editor.putString("loginUserName","");  //清除登录时的用户名
        editor.commit();    //提交修改
    }

}

2、我的界面

(1)、创建我的界面

res/layout文件夹中,创建布局文件main_view_myinfo.xml

(2)、导入界面图片

将所需图片 myinfo_login_bg.pngcoures_history_icon.pngmyinfo_setting_icon.pngiv_right_arrow.png导入drawable文件夹中。

(3)、我的界面代码——main_view_myinfo.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <LinearLayout
        android:id="@+id/ll_head"
        android:layout_width="fill_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:src="@drawable/default_icon"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="75dp"/>
      
        <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="fill_parent"
        android:layout_height="1dp"
        android:background="#E3E3E3"
        android:layout_marginTop="20dp"></View>
    
    <RelativeLayout
        android:id="@+id/rl_course_history"
        android:background="#F7F8F8"
        android:gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_width="fill_parent"
        android:layout_height="50dp">
        <ImageView
            android:id="@+id/iv_course_historyicon"
            android:src="@drawable/course_history_icon"
            android:layout_marginLeft="20dp"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_centerVertical="true"/>
        <TextView
            android:id="@+id/tv_course_history"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="播放记录"
            android:textColor="#a3a3a3"
            android:layout_toRightOf="@+id/iv_course_historyicon"
            android:layout_centerVertical="true"
            android:layout_marginLeft="25dp"
            android:textSize="16sp"/>
        <ImageView
            android:src="@drawable/iv_right_arrow"
            android:layout_marginRight="25dp"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_width="15dp"
            android:layout_height="15dp" />
    </RelativeLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#E3E3E3"></View>
    <RelativeLayout
        android:id="@+id/rl_setting"
        android:background="#F7F8F8"
        android:gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_width="fill_parent"
        android:layout_height="50dp">
        <ImageView
            android:id="@+id/iv_userinfo_icon"
            android:src="@drawable/myinfo_setting_icon"
            android:layout_marginLeft="20dp"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_centerVertical="true"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="设置"
            android:textColor="#a3a3a3"
            android:layout_toRightOf="@+id/iv_userinfo_icon"
            android:layout_centerVertical="true"
            android:layout_marginLeft="25dp"
            android:textSize="16sp"/>
        <ImageView
            android:src="@drawable/iv_right_arrow"
            android:layout_marginRight="25dp"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_width="15dp"
            android:layout_height="15dp" />
    </RelativeLayout>
    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#E3E3E3"></View>
</LinearLayout>

(4)、创建AnalysisUtils工具类

utils包中创建一个java类,命名为AnalysisUtils,代码如下
package com.boxuegu.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class AnalysisUtils {
	/* 
	 * 从SharedPreferences中读取用户名 
	 */
	 public static String readLoginUserName(Context context){
	        SharedPreferences sp = context.getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
	        String userName = sp.getString("loginUserName","");
	        return userName;
	    }
}

(5)、我的界面逻辑代码

中com.boxuegu包,创建一个view包,在view包中创建一个java类,命名为MyInfoView。代码如下
package com.boxuegu.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.boxuegu.R;
import com.boxuegu.activity.LoginActivity;
import com.boxuegu.activity.SettingActivity;
import com.boxuegu.utils.AnalysisUtils;



public class MyInfoView {
    public ImageView iv_head_icon;
    private LinearLayout ll_head;
    private RelativeLayout rl_course_history,rl_setting;
    private TextView tv_user_name;
    private Activity mContext;
    private LayoutInflater mInflater;
    private View mCurrentView;
    public MyInfoView(Activity context){
    	//为之后将layout转化为view时用
        mContext= context;
        mInflater= LayoutInflater.from(mContext);
    }
    private void createView(){
        initView();
    }

	/* 
	 * 获取界面控件
	 */
    private void initView() {
    	//设置布局文件
        mCurrentView = mInflater.inflate(R.layout.main_view_myinfo,null);
        ll_head = (LinearLayout)mCurrentView.findViewById(R.id.ll_head);
        iv_head_icon = (ImageView) mCurrentView.findViewById(R.id.iv_head_icon);
        rl_course_history = (RelativeLayout) mCurrentView.findViewById(R.id.rl_course_history);
        rl_setting = (RelativeLayout) mCurrentView.findViewById(R.id.rl_setting);
        tv_user_name = (TextView) mCurrentView.findViewById(R.id.tv_user_name);
        mCurrentView.setVisibility(View.VISIBLE);
        setLoginParams(readLoginStatus());
        ll_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            	//判断是否登录
                if (readLoginStatus()){
                    //已登录跳转到个人资料界面
                }else{
                	//未登录跳转到登录界面
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    mContext.startActivityForResult(intent,1);
                }
            }
        });
        rl_course_history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (readLoginStatus()){
                    //跳转到播放记录界面
                }else{
                    Toast.makeText(mContext,"您还未登录,请先登录",Toast.LENGTH_SHORT).show();
                }
            }
        });
        rl_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (readLoginStatus()){
                	//跳转到设置界面 
                }else{
                    Toast.makeText(mContext,"您还未登录,请先登录",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
    
	/* 
	 * 登录成功后设置我的界面
	 */
    public void setLoginParams(boolean isLogin){
        if (isLogin){
            tv_user_name.setText(AnalysisUtils.readLoginUserName(mContext));
        }
        else{
            tv_user_name.setText("点击登录");
        }
    }
    
    /* 
	 * 获取当前在导航栏上方对应的view
	 */
    public View getView(){
        if (mCurrentView==null){
            createView();
        }return mCurrentView;
    }
    
    /* 
	 * 显示当前导航栏上方所对应的view界面
	 */
    public void showView(){
        if (mCurrentView==null){
            createView();
        }
        mCurrentView.setVisibility(View.VISIBLE);
    }

    /* 
	 * 从SharedPreferences中读取登录状态
	 */
    private boolean readLoginStatus(){
        SharedPreferences sp = mContext.getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        boolean isLogin = sp.getBoolean("isLogin",false);
        return isLogin;
    }

}

(6)、修改MainActivity.java

在文件中的40行代码下面加入如下代码,导入相应的包
private MyInfoView mMyInfoView;
找到MainActivity.java的createView方法,当case为2时,在注释//我的界面下方添加如下代码
if (mMyInfoView == null){
                    mMyInfoView = new MyInfoView(this);
                    mBodyLayout.addView(mMyInfoView.getView());
                }else {
                    mMyInfoView.getView();
                }
                mMyInfoView.showView();
修改登录成功时的代码,在MainActivity.java中加入以下代码
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data!=null){
        	//从设置界面或登录界面传递过来的登录状态
            boolean isLogin = data.getBooleanExtra("isLogin",false);
            if (isLogin){      //登录成功时显示课程界面
                clearBottomImageState();
                selectDisplayView(0);
            }
            if (mMyInfoView != null){    //登录成功或退出登录时根据islogin设置我的界面
                mMyInfoView.setLoginParams(isLogin);
            }
        }
    }

二、设置界面

1、创建设置界面

com.boxuegu.activity包中新建一个类,命名为SettingActivity。在res/layout文件夹下新建布局文件,命名为activity_setting

2、设置界面代码activity_setting.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">
    <include layout="@layout/main_title_bar"/>
    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="15dp"
        android:background="#E3E3E3"/>
    <RelativeLayout
        android:id="@+id/rl_modify_psw"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="#F7F8F8"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="25dp"
            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="fill_parent"
        android:layout_height="1dp"
        android:background="#E3E3E3"/>
    <RelativeLayout
        android:id="@+id/rl_security_setting"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="#F7F8F8"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="25dp"
            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="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="15dp"
        android:background="#E3E3E3"/>
    <RelativeLayout
        android:id="@+id/rl_exit_login"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="#F7F8F8"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="25dp"
            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="fill_parent"
        android:layout_height="1dp"
        android:background="#E3E3E3"/>
</LinearLayout>

3、设置界面逻辑代码——SettingActivity.java

package com.boxuegu.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.boxuegu.R;

public class SettingActivity extends AppCompatActivity {
    public static  SettingActivity instance = null;
    private TextView tv_main_title;
    private TextView tv_back;
    private RelativeLayout rl_title_bar;
    private RelativeLayout rl_modify_psw;
    private RelativeLayout rl_security_setting;
    private RelativeLayout rl_exit_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置界面布局
        setContentView(R.layout.activity_setting);
        //设置界面为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        instance = this;
        init();
    }

    //获取界面控件
    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);
        rl_title_bar = (RelativeLayout) findViewById(R.id.title_bar);
        rl_title_bar.setBackgroundColor(Color.parseColor("#FF9900"));
        rl_modify_psw = (RelativeLayout) findViewById(R.id.rl_modify_psw);
        rl_security_setting = (RelativeLayout) findViewById(R.id.rl_security_setting);
        rl_exit_login = (RelativeLayout) findViewById(R.id.rl_exit_login);
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SettingActivity.this.finish();
            }
        });
        
        //修改密码的点击事件
        rl_modify_psw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到修改密码界面
            }
        });
        
        //设置密保的点击事件
        rl_security_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到设置密保界面
            }
        });
        
        //退出登录的点击事件
        rl_exit_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(SettingActivity.this,"退出登录成功",Toast.LENGTH_SHORT).show();
                clearLoginStatus();   //清除登录状态和登录的用户名
                //退出成功后把退出成功的状态传递到MainActivity中
                Intent data = new Intent();
                data.putExtra("isLogin",false);
                setResult(RESULT_OK,data);
                SettingActivity.this.finish();
            }
        });
    }

    //清除SharedPreferences中的登录状态和登录时的用户名
    private void clearLoginStatus(){
        SharedPreferences sp = getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();  //获取编辑器
        editor.putBoolean("isLogin",false);   //清除登录状态
        editor.putString("loginUserName","");  //清除用户名
        editor.commit();  //提交修改
    }
}

4、修改我的界面代码

MyInfoView.java文件中的initView方法,找到注释//跳转到设置界面下方添加如下代码
 Intent intent = new Intent(mContext, SettingActivity.class);
 mContext.startActivityForResult(intent,1);

三、运行效果

image

Android项目实战系列—基于博学谷 开源地址

image               
image

0

评论