Education system app (4)

Time:2021-6-20

My module
Master the development of password modification function, and realize the modification of user password
Master the development of setting security function, and through security can retrieve the user password

Challenge task
The first time you open a page is fragment_ 1. Please modify the relevant code to make the initial page fragment_ 3. Don’t forget to change the color of the navigation bar at the bottom

Solution
Just change mainactivity.java

package cn.edu.gdmec.android.boxuegu.activity;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.edu.gdmec.android.boxuegu.R;
import cn.edu.gdmec.android.boxuegu.fragment.CourseFragment;
import cn.edu.gdmec.android.boxuegu.fragment.ExercisesFragment;
import cn.edu.gdmec.android.boxuegu.fragment.MyinfoFragment;

/*The task is displayed in the Hello world position of the main interface: user name + "login succeeded"*/
public class MainActivity extends FragmentActivity implements View.OnClickListener{
    private RelativeLayout main_body;
    private TextView bottom_bar_text_course;
    private ImageView bottom_bar_image_course;
    private RelativeLayout bottom_bar_course_btn;
    private TextView bottom_bar_text_exercises;
    private ImageView bottom_bar_image_exercises;
    private RelativeLayout bottom_bar_exercises_btn;
    private TextView bottom_bar_text_myinfo;
    private ImageView bottom_bar_image_myinfo;
    private RelativeLayout bottom_bar_myinfo_btn;
    private LinearLayout main_bottom_bar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //The code to add fragment to activity is as follows
       /* FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.main_body,new CourseFragment()).commit();*/
       setMain();
    }

    private void setMain() {
        this.getSupportFragmentManager().beginTransaction().add(R.id.main_body,new MyinfoFragment()).commit();
        setSelectStatus(2);
    }

    private void setSelectStatus(int index) {
        switch (index){
            case 0:
                bottom_bar_image_course.setImageResource(R.drawable.main_course_icon_selected);
                bottom_bar_text_course.setTextColor(Color.parseColor("#0097F7"));
                bottom_bar_text_exercises.setTextColor(Color.parseColor("#666666"));
                bottom_bar_text_myinfo.setTextColor(Color.parseColor("#666666"));
                bottom_bar_image_exercises.setImageResource(R.drawable.main_exercises_icon);
                bottom_bar_image_myinfo.setImageResource(R.drawable.main_my_icon);
                break;
            case 1:
                bottom_bar_image_exercises.setImageResource(R.drawable.main_exercises_icon_selected);
                bottom_bar_text_exercises.setTextColor(Color.parseColor("#0097F7"));
                bottom_bar_text_course.setTextColor(Color.parseColor("#666666"));
                bottom_bar_text_myinfo.setTextColor(Color.parseColor("#666666"));
                bottom_bar_image_course.setImageResource(R.drawable.main_course_icon);
                bottom_bar_image_myinfo.setImageResource(R.drawable.main_my_icon);
                break;
            case 2:
                bottom_bar_image_myinfo.setImageResource(R.drawable.main_my_icon_selected);
                bottom_bar_text_myinfo.setTextColor(Color.parseColor("#0097F7"));
                bottom_bar_text_course.setTextColor(Color.parseColor("#666666"));
                bottom_bar_text_exercises.setTextColor(Color.parseColor("#666666"));
                bottom_bar_image_exercises.setImageResource(R.drawable.main_exercises_icon);
                bottom_bar_image_course.setImageResource(R.drawable.main_course_icon);
                break;
        }
    }
    private void initView() {
        main_body = findViewById(R.id.main_body);
        bottom_bar_text_course = findViewById(R.id.bottom_bar_text_course);
        bottom_bar_image_course = findViewById(R.id.bottom_bar_image_course);
        bottom_bar_course_btn = findViewById(R.id.bottom_bar_course_btn);
        bottom_bar_text_exercises = findViewById(R.id.bottom_bar_text_exercises);
        bottom_bar_image_exercises = findViewById(R.id.bottom_bar_image_exercises);
        bottom_bar_exercises_btn = findViewById(R.id.bottom_bar_exercises_btn);
        bottom_bar_text_myinfo = findViewById(R.id.bottom_bar_text_myinfo);
        bottom_bar_image_myinfo = findViewById(R.id.bottom_bar_image_myinfo);
        bottom_bar_myinfo_btn = findViewById(R.id.bottom_bar_myinfo_btn);
        main_bottom_bar = findViewById(R.id.main_bottom_bar);

        bottom_bar_course_btn.setOnClickListener(this);
        bottom_bar_exercises_btn.setOnClickListener(this);
        bottom_bar_myinfo_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bottom_bar_course_btn:
                getSupportFragmentManager().beginTransaction().add(R.id.main_body,new CourseFragment()).commit();
                setSelectStatus(0);
                break;
            case R.id.bottom_bar_exercises_btn:
                getSupportFragmentManager().beginTransaction().add(R.id.main_body,new ExercisesFragment()).commit();
                setSelectStatus(1);
                break;
            case R.id.bottom_bar_myinfo_btn:
                getSupportFragmentManager().beginTransaction().add(R.id.main_body,new MyinfoFragment()).commit();
                setSelectStatus(2);
                break;
        }
    }
}

App (4)
“I” module of the erudite Valley Project

Learning objectives
Master the development of password modification function, and realize the modification of user password;
Master the development of setting security function, and through security can retrieve the user password.

Project practice
My interface
set up
Change Password
Set security and retrieve password
Task implementation
Fix small bug of fragment
Modify security settings page
Optimize password retrieval

“I” module is mainly to set up users and ensure the safety of users. When the user logs in successfully, the password can be changed and the security can be set, and only the account that has set the security can retrieve the password.

My interface
The first is the resource image. Myinfo is used in my interface_ login_ bg、course_ history_ icon、iv_ right_ arrow、myinfo_ setting_ icon

As usual, put them under drawable

Education system app (4)

Then there is the layout code, the fragment created in our last lesson_ Myinfo with code fragment_ myinfo.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="@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= "Click login"
            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= "Playback record"
            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= "Settings"
            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>

After the modification, we try to run it. Click “I” in the navigation bar at the bottom, and a page without interaction will come out.

After the “I” interface can be displayed, let’s write a tool class.
Because the shared preferences parameter is used many times in the project to store or clear the login status of users, the “I” interface also requires the method of reading the user’s name, so let’s just throw these three methods into analysis utils.
Create a new Java class called analysis utils in the utils package.

After the creation, we write readloginstatus (), clearloginstatus (), and
Readloginusername() has three methods.

AnalysisUtils.java

package cn.edu.gdmec.android.boxuegu.utils;

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

public class AnalysisUtils {
    //Read user name
    public static String readLoginUserName(Context context){
        SharedPreferences sharedPreferences = context.getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
        String userName=sharedPreferences.getString("loginUserName","");
        return userName;
    }

    //Read login status
    public static boolean readLoginStatus(Context context){
        SharedPreferences sharedPreferences = context.getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
        boolean isLogin=sharedPreferences.getBoolean("isLogin",false);
        return isLogin;
    }

    //Clear login status
    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();
    }
}

When we need to use these methods in the future, just call them.
After writing the tool class, let’s continue to improve the code of my page.
Add the view. Onclicklistener interface to myinfofragment.

public class MyinfoFragment extends Fragment implements View.OnClickListener{

In the red area, ALT + carriage return generates onclick () method and adds the ID to respond.

MyinfoFragment.java

@Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_head:
                break;
            case R.id.rl_course_history:
                break;
            case R.id.rl_setting:
                break;
        }
    }

Don’t forget the monitor.

MyinfoFragment.java

    @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);
    }

The avatar part of “I” interface has two functions

After opening the “I” interface, you need to judge whether you have logged in. If you have logged in, the user name will be displayed. If you have not logged in, the “click login” will be displayed.
After clicking the avatar part, you will judge whether to log in. If you log in, you will jump to the profile interface. If you don’t log in, you will jump to the login page.
Let’s finish the first function first
We write an if else statement in onviewcreate() to display the corresponding text under the same image according to the current login status.
The content of judgment can call the tool class we just wrote.

MyinfoFragment.java

@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 ("click login");
        }

        llHead.setOnClickListener(this);
        rlCourseHistory.setOnClickListener(this);
        rlSetting.setOnClickListener(this);
    }

Then in onclick () method, add judgment to the click of the avatar part and other buttons.

MyinfoFragment.java

@Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_head:
                if (AnalysisUtils.readLoginStatus(getActivity())){
                    //Jump to profile interface
                }else {
                    //Jump to login interface
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    getActivity().startActivityForResult(intent,1);
                }
                break;
            case R.id.rl_course_history:
                if (AnalysisUtils.readLoginStatus(getActivity())){
                    //Jump to the playback record page
                }else {
                    Toast. Maketext (getactivity(), "you are not logged in, please log in first", toast. Length_ SHORT).show();
                }
                break;
            case R.id.rl_setting:
                if (AnalysisUtils.readLoginStatus(getActivity())){
                    //Jump to the setting interface
                }else {
                    Toast. Maketext (getactivity(), "you are not logged in, please log in first", toast. Length_ SHORT).show();
                }
                break;
        }
    }

Note: when you are not logged in, click login to open the login page. Mainactivity needs to update the status and display it, so use the startactivityforresult() method to open it. As usual, you open it with startactivityforresult(), and mainactivity needs to add onactivityforresult() method to execute the response.

In mainactivity, press Ctrl + O, type “onActivity”, find the onactivityforresult method, and click generate code.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

Add code to it: judge the login status passed from loginactivity, and execute the response action.

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){
                setSelectStatus(0);
            }
            else {
                setSelectStatus(2);
            }
        }
    }

OK, in order to check the effect, we add a method to exit and clear the login status to mainactivity. If you click back twice in succession, you will exit. If the interval between two clicks exceeds 2 seconds, you will be prompted to press again to exit.

MainActivity.java

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, "press again to exit the erudite Valley", toast. Length_ SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                this.finish();
                if (AnalysisUtils.readLoginStatus(this)) {
                    AnalysisUtils.cleanLoginStatus(this);
                }
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

Then we run the next program, login completed, “I” interface should display the user name.
At this time, you will find that after you click “click login”, the following page will be displayed. After you click the bottom navigation bar, it will be displayed normally. But it will be different to finish the last task~

This is a common fragment refresh problem. Take it as a small bug, as today’s “task implementation”, and solve it.

set up
Next, let’s do the setup interface.
Create a new layout file named activity_ setting。

Add the layout code.
activity_setting.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="@android:color/white"
    android:orientation="vertical">
    <!-- The title display settings -- >
    <include layout="@layout/main_title_bar" />
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="15dp"
        android:background="#E3E3E3" />
    <RelativeLayout
        android:id="@+id/rl_modify_psw"
        android:layout_width="match_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= "Change password"
            android:textColor="#A3A3A3"
            android:textSize="16sp" />
        <ImageView
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:paddingRight="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_security_setting"
        android:layout_width="match_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= "Set security"
            android:textColor="#A3A3A3"
            android:textSize="16sp" />
        <ImageView
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:paddingRight="25dp"
            android:src="@drawable/iv_right_arrow" />
    </RelativeLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#E3E3E3" />
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="15dp"
        android:background="#E3E3E3" />
    <RelativeLayout
        android:id="@+id/rl_exit_login"
        android:layout_width="match_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= "Log out"
            android:textColor="#A3A3A3"
            android:textSize="16sp" />
    </RelativeLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#E3E3E3" />
</LinearLayout>

Create a new activity
For TV_ main_ Title set a text. To title_ Add a background. Add the view. Onclicklistener interface. Alt + enter generates onclick() code. Add several buttons to onclick() to set the interface. Add listeners to a few buttons.

SettingActivity.java

package cn.edu.gdmec.android.boxuegu.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.gdmec.android.boxuegu.R;
import cn.edu.gdmec.android.boxuegu.utils.AnalysisUtils;

public class SettingActivity extends Activity implements View.OnClickListener{
    private TextView tv_back;
    private TextView tv_main_title;
    private TextView tv_save;
    private RelativeLayout title_bar;
    private RelativeLayout rl_modify_psw;
    private RelativeLayout rl_security_setting;
    private RelativeLayout rl_exit_login;
    public static SettingActivity instance=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        instance=this;
        initView();
    }
    private void initView(){
        tv_back=findViewById(R.id.tv_back);
        tv_main_title=findViewById(R.id.tv_main_title);
        tv_save=findViewById(R.id.tv_save);
        title_bar=findViewById(R.id.title_bar);
        rl_modify_psw=findViewById(R.id.rl_modify_psw);
        rl_security_setting=findViewById(R.id.rl_security_setting);
        rl_exit_login=findViewById(R.id.rl_exit_login);

        tv_ main_ Title. Settext ("Settings");
        title_bar.setBackgroundColor(Color.parseColor("#30B4FF"));

        tv_back.setOnClickListener(this);
        rl_modify_psw.setOnClickListener(this);
        rl_security_setting.setOnClickListener(this);
        rl_exit_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back:
                SettingActivity.this.finish();
                break;
            case R.id.rl_modify_psw:
                //Password modification interface
                break;
            case R.id.rl_security_setting:
                //Set security interface
                break;
            case R.id.rl_exit_login:
                //Log out to clear the login status
                Toast.maketext (this, "log out successfully", toast.length_ SHORT).show();
                AnalysisUtils.cleanLoginStatus(this);
                Intent data=new Intent();
                data.putExtra("isLogin",false);
                setResult(RESULT_OK,data);
                finish();
                break;
        }
    }
}

Myinfofragrnt. Java add the jump code.

@Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_head:
                if (AnalysisUtils.readLoginStatus(getActivity())){
                    //Jump to profile interface
                }else {
                    //Jump to login interface
                }
                break;
            case R.id.rl_course_history:
                if (AnalysisUtils.readLoginStatus(getActivity())){
                    //Jump to the playback record page
                }else {
                    Toast. Maketext (getactivity(), "you are not logged in, please log in first", toast. Length_ SHORT).show();
                }
                break;
            case R.id.rl_setting:
                if (AnalysisUtils.readLoginStatus(getActivity())){
                    //Jump to the setting interface
                    Intent intent=new Intent(getActivity(), SettingActivity.class);
                    getActivity().startActivityForResult(intent,1);
                }else {
                    Toast. Maketext (getactivity(), "you are not logged in, please log in first", toast. Length_ SHORT).show();
                }
                break;
        }
    }

Don’t forget to declare settingactivity in androidmanifest.xml, run the program, and click settings in my interface to open the settings page.

Change Password
The first is to modify the layout code of the password interface
Create a new layout file named activity_ modify_ psw

Go straight to the code.
activity_modify_psw.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Change password -- >
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/register_bg"
    android:orientation="vertical">
    <include layout="@layout/main_title_bar"/>
    <EditText
        android:id="@+id/et_original_psw"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/register_user_name_bg"
        android:layout_marginTop="35dp"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:drawableLeft="@drawable/psw_icon"
        android:drawablePadding="10dp"
        android:gravity="center_vertical"
        android:hint= "Please enter the original password"
        android:inputType="textPassword"
        android:paddingLeft="8dp"
        android:textColor="#000000"
        android:textColorHint="#a3a3a3"
        android:textSize="14sp"
        android:singleLine="true"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        />
    <EditText
        android:id="@+id/et_new_psw"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/register_user_name_bg"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:drawableLeft="@drawable/psw_icon"
        android:drawablePadding="10dp"
        android:gravity="center_vertical"
        android:hint= "Please enter a new password"
        android:inputType="textPassword"
        android:paddingLeft="8dp"
        android:textColor="#000000"
        android:textColorHint="#a3a3a3"
        android:textSize="14sp"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        />
    <EditText
        android:id="@+id/et_new_psw_again"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/register_user_name_bg"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:drawableLeft="@drawable/psw_icon"
        android:drawablePadding="10dp"
        android:gravity="center_vertical"
        android:hint= "Please enter the new password again"
        android:inputType="textPassword"
        android:paddingLeft="8dp"
        android:singleLine="true"
        android:textColor="#000000"
        android:textColorHint="#a3a3a3"
        android:textSize="14sp"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        />
    <Button
        android:id="@+id/btn_save"
        android:layout_gravity="center_horizontal"
        android:textSize="18sp"
        android:textColor="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:background="@drawable/register_selector"
        android:text= "Save" / >
</LinearLayout>

Generate activity file, modifypswactivity. Java
Note: in the submit method, after the password is successfully modified, not only the current page is closed, but also the setting interface is closed, so instance is used

package cn.edu.gdmec.android.boxuegu.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.gdmec.android.boxuegu.R;
import cn.edu.gdmec.android.boxuegu.utils.AnalysisUtils;
import cn.edu.gdmec.android.boxuegu.utils.MD5Utils;

public class ModifyPswActivity extends Activity implements View.OnClickListener {
    private TextView tv_back;
    private TextView tv_main_title;
    private TextView tv_save;
    private RelativeLayout title_bar;
    private EditText et_original_psw;
    private EditText et_new_psw;
    private EditText et_new_psw_again;
    private Button btn_save;
    private String userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_psw);
        initView();
        userName = AnalysisUtils.readLoginUserName(this);
    }

    private void initView() {
        tv_back=findViewById(R.id.tv_back);
        tv_main_title=findViewById(R.id.tv_main_title);
        tv_save=findViewById(R.id.tv_save);
        title_bar=findViewById(R.id.title_bar);
        et_original_psw=findViewById(R.id.et_original_psw);
        et_new_psw=findViewById(R.id.et_new_psw);
        et_new_psw_again=findViewById(R.id.et_new_psw_again);
        btn_save=findViewById(R.id.btn_save);
        tv_ main_ Title. Settext ("change password");
        btn_save.setOnClickListener(this);
        tv_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_save:
                submit();
                break;
            case R.id.tv_back:
                finish();
                break;
        }
    }

    private void submit() {
        String psw=et_original_psw.getText().toString().trim();
        String newPsw=et_new_psw.getText().toString().trim();
        String again=et_new_psw_again.getText().toString().trim();
        if (TextUtils.isEmpty(psw)){
            Toast.maketext (this, "please enter the original password", toast.length_ SHORT).show();
            return;
        }else if (!MD5Utils.md5(psw).equals(readPsw())){
            Log.i("MD5Utils.md5(psw)",""+MD5Utils.md5(psw));
            Log.i("readPsw",""+readPsw());
            Toast. Maketext (this), "the password you entered is inconsistent with the original password", toast. Length_ SHORT).show();
            return;
        }else if (MD5Utils.md5(newPsw).equals(readPsw())){
            Toast. Maketext (this), "the new password you entered is not consistent with the original password", toast. Length_ SHORT).show();
            return;
        }else if (TextUtils.isEmpty(psw)){
            Toast.maketext (this, "please enter password", toast.length_ SHORT).show();
            return;
        }else if (TextUtils.isEmpty(again)){
            Toast.maketext (this, "please enter the new password again", toast.length_ SHORT).show();
        }else if (!newPsw.equals(again)){
            Toast. Maketext (this, "the new password entered again is inconsistent", toast. Length_ SHORT).show();
            return;
        }else {
            Toast.maketext (this, "new password set successfully", toast.length_ SHORT).show();
            modifyPsw(newPsw);//
            Intent intent=new Intent(ModifyPswActivity.this,LoginActivity.class);
            startActivity(intent);
            //Close settings page
            //In the submit method, after the password is successfully modified, not only the current page is closed, but also the setting interface is closed, so instance is used
            SettingActivity.instance.finish();
            //Close the password change page
            ModifyPswActivity.this.finish();
        }
    }

    private void modifyPsw(String newPsw) {
        String md5psw= MD5Utils.md5(newPsw);
        SharedPreferences sharedPreferences=getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(userName,md5psw);
        editor.commit();
    }

    private String readPsw() {
        SharedPreferences sharedPreferences=getSharedPreferences("loginInfo",MODE_PRIVATE);
        String spPsw=sharedPreferences.getString(userName,"");
        Log.i("username",userName);
        Log.i("spPsw",spPsw);
        return spPsw;
    }
}

Note: in the submit method, after the password is successfully modified, not only the current page is closed, but also the setting interface is closed, so instance is used

Settingactivity. Java, create an instance for it

SettingActivity.java

public class SettingActivity extends Activity implements View.OnClickListener{
    private TextView tv_back;
    private TextView tv_main_title;
    private TextView tv_save;
    private RelativeLayout title_bar;
    private RelativeLayout rl_modify_psw;
    private RelativeLayout rl_security_setting;
    private RelativeLayout rl_exit_login;
    public static SettingActivity instance=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        instance=this;
        initView();
    }

Write the jump method in onclick().

SettingActivity.java

@Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back:
                SettingActivity.this.finish();
                break;
            case R.id.rl_modify_psw:
                //Password modification interface
                Intent intent=new Intent(SettingActivity.this,ModifyPswActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_security_setting:
                //Set security interface
                break;
            case R.id.rl_exit_login:
                //Log out to clear the login status
                Toast.maketext (this, "log out successfully", toast.length_ SHORT).show();
                AnalysisUtils.cleanLoginStatus(this);
                Intent data=new Intent();
                data.putExtra("isLogin",false);
                setResult(RESULT_OK,data);
                finish();
                break;
        }
    }

Finally, declare the activity in androidmanifest.xml and run the program to see the effect

Set security and retrieve password
The image resources needed to set the password protection and retrieve the password page are
find_psw_icon.png

Create a new layout file named activity_ find_ psw。

Code of layout file:
activity_find_psw.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Retrieve password -- >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_bg"
    android:orientation="vertical">
    <!-- To retrieve the password, you need to set the title bar -- >
    <include layout="@layout/main_title_bar" />
    <TextView
        android:id="@+id/tv_user_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="35dp"
        android:text= "What is your user name?"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        android:visibility="gone" />
    <EditText
        android:id="@+id/et_user_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/find_psw_icon"
        android:hint= "Please enter your user name"
        android:inputType="text"
        android:paddingLeft="8dp"
        android:textColor="#000000"
        android:textColorHint="#A3A3A3"
        android:visibility="gone" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="15dp"
        android:text= "What's your name?"
        android:textColor="@android:color/white"
        android:textSize="18sp"/>
    <EditText
        android:id="@+id/et_validate_name"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/find_psw_icon"
        android:hint= "Please enter the name to verify"
        android:inputType="text"
        android:paddingLeft="8dp"
        android:textColor="#000000"
        android:textColorHint="#A3A3A3" />
    <TextView
        android:id="@+id/tv_reset_psw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="15dp"
        android:text= "Please enter a new password"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        android:visibility="gone"/>
    <Button
        android:id="@+id/btn_validate"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="15dp"
        android:background="@drawable/register_selector"
        android:text= "Validation"
        android:textColor="@android:color/white"
        android:textSize="18sp" />
</LinearLayout>

Generate activity file, findpswactivity. Java

FindPswActivity.java

package cn.edu.gdmec.android.boxuegu.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.gdmec.android.boxuegu.R;
import cn.edu.gdmec.android.boxuegu.utils.AnalysisUtils;
import cn.edu.gdmec.android.boxuegu.utils.MD5Utils;

public class FindPswActivity extends Activity implements View.OnClickListener {

    private TextView tv_back;
    private TextView tv_main_title;
    private RelativeLayout title_bar;
    private TextView tv_user_name;
    private EditText et_user_name;
    private EditText et_validate_name;
    private TextView tv_reset_psw;
    private Button btn_validate;
    private String from;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_psw);
        from=getIntent().getStringExtra("from");
        initView();
    }

    private void initView() {
        tv_back=findViewById(R.id.tv_back);
        tv_main_title=findViewById(R.id.tv_main_title);
        title_bar=findViewById(R.id.title_bar);
        tv_user_name=findViewById(R.id.tv_user_name);
        et_user_name=findViewById(R.id.et_user_name);
        et_validate_name=findViewById(R.id.et_validate_name);
        tv_reset_psw=findViewById(R.id.tv_reset_psw);

        btn_validate=findViewById(R.id.btn_validate);
        if ("security".equals(from)){
            tv_ main_ Title. Settext ("set security");
        }else{
            tv_ main_ Title. Settext ("retrieve password");
            tv_user_name.setVisibility(View.VISIBLE);
            et_user_name.setVisibility(View.VISIBLE);
        }
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FindPswActivity.this.finish();
            }
        });
        btn_validate.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_validate:
                submit();
                break;
        }
    }
    private void submit() {
        // validate
        String validateName = et_validate_name.getText().toString().trim();
        If ("security". Equals (from)) {// set security
            if (TextUtils.isEmpty(validateName)) {
                Toast.maketext (this, "please enter the name to verify", toast.length_ SHORT).show();
                return;
            } else {
                Toast.maketext (this, "security set successfully", toast.length_ SHORT).show();
                //Save to
                saveSecurity(validateName);
                FindPswActivity.this.finish();
                return;
            }
        }else {
            final String name=et_user_name.getText().toString().trim();
            String sp_security=readSecurity(name);
            if (TextUtils.isEmpty(name)){
                Toast.maketext (this, "please enter your user name", toast.length_ SHORT).show();
                return;
            }else if (!isExistUserName(name)){
                Toast. Maketext (this, "the user name you entered does not exist", toast. Length_ SHORT).show();
                return;
            }else if (TextUtils.isEmpty(validateName)){
                Toast.maketext (this, "please enter the name to verify", toast.length_ SHORT).show();
                return;
            }else if (!validateName.equals(sp_security)){
                Toast.maketext (this, "the input security is incorrect", toast.length_ SHORT).show();
                return;
            }else {
                tv_reset_psw.setVisibility(View.VISIBLE);
                tv_ reset_ PSW. Settext ("initial password: 123456");
                savePsw(name);
            }
        }
    }
    /**
     *Save initialization password
     **/
    private void savePsw(String name) {
        String md5Psw= MD5Utils.md5("123456");
        SharedPreferences sharedPreferences = getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(name,md5Psw);
        editor.commit();
    }

    private boolean isExistUserName(String name){
        boolean hasUserName=false;
        SharedPreferences sharedPreferences=getSharedPreferences("loginInfo",MODE_PRIVATE);
        String spPsw=sharedPreferences.getString(name,"");
        if (!TextUtils.isEmpty(spPsw)){
            hasUserName=true;
        }
        return hasUserName;
    }
    /**
     *Read security
     **/
    private String readSecurity(String name){
        SharedPreferences sharedPreferences=getSharedPreferences("loginInfo",MODE_PRIVATE);
        String security=sharedPreferences.getString(name+"_security","");
        return security;
    }
    /**
     *Keep the name of the security guard
     **/
    private void saveSecurity(String validateName) {
        SharedPreferences sharedPreferences=getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString(AnalysisUtils.readLoginUserName(this)+"_security",validateName);
        editor.commit();
    }
}

We put the password recovery and password setting in one activity, so we need to add jump methods to the login interface and the setting interface.
Find TV in init() method of loginactivity_ find_ psw.setOnClickListener。

LoginActivity.java

//Retrieve the click event of password control
        tv_find_psw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Jump to the password retrieval interface (this page has not been created yet)
                Intent intent=new Intent(LoginActivity.this,FindPswActivity.class);
                startActivity(intent);
            }
        });

Add jump code to onclick() method of settingactivity

@Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back:
                SettingActivity.this.finish();
                break;
            case R.id.rl_modify_psw:
                //Password modification interface
                Intent intent=new Intent(SettingActivity.this,ModifyPswActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_security_setting:
                //Set security interface
                Intent intent1=new Intent(SettingActivity.this,FindPswActivity.class);
                intent1.putExtra("from","security");
                startActivity(intent1);
                break;
            case R.id.rl_exit_login:
                //Log out to clear the login status
                Toast.maketext (this, "log out successfully", toast.length_ SHORT).show();
                AnalysisUtils.cleanLoginStatus(this);
                Intent data=new Intent();
                data.putExtra("isLogin",false);
                setResult(RESULT_OK,data);
                finish();
                break;
        }
    }

Finally, the activity is declared in androidmanifest. XML

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.edu.gdmec.android.boxuegu">

    <!-- originally be android:theme= "@style/AppTheme"-->
    <!-- Remove the title bar of actionbar -- >
    <!-- Add application icon, app_ icon-->
    <application
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <activity android:name=".activity.SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- Add implementation class -- >
        <activity android:name=".activity.MainActivity"></activity>
        <activity android:name=".activity.LoginActivity"></activity>
        <activity android:name=".activity.RegisterActivity"></activity>
        <activity android:name=".activity.SettingActivity"></activity>
        <activity android:name=".activity.ModifyPswActivity"></activity>
        <activity android:name=".activity.FindPswActivity"></activity>
    </application>
</manifest>

Task implementation
Fix small bug of fragment
In the non login state, click “click login”. After login, the “I” interface should be displayed, and the user name should be displayed normally.

In the login state, click “Settings” – > Exit login, the “I” interface should continue to be displayed, and the user name should be displayed normally.

Modify security settings page
The button on the security settings page shows “verification”, which should be more reasonable.
Change the text on the button to “set” and request to modify the password. The interface button is still “verify”.

Optimize password retrieval
The password recovery function of the login interface uses the initialization password, which is required to be changed to a new password.
Note: when the new password box is displayed, the text of the button should also be changed!

Effect after change:

Education system app (4)

summary
This is a good Android four steps!

❤️ Don’t forget to leave your footprints [like + collect + comment]

Author Info:

[author]: jeskson
Official account: dada’s front bar.
[welfare]: the official account answers the “information”, sends the gift package of self study materials (enter group to share, want to say anything ha, see if I have).
[reprint description]: please explain the source of reprint, thank you for your cooperation~

Big front-end development, positioning front-end development technology stack, blog, PHP background knowledge points, web full stack technology field, data structure and algorithm, network principle, etc. are presented to small partners easily. Thank you for your support and love!!!


If the content of this number is not in place (such as copyright or other issues), please contact us in time for rectification, and we will deal with it at the first time.


Please like it! Because your approval / encouragement is the biggest driving force of my writing!

Welcome to pay attention to dada’s CSDN!

This is a blog with quality and attitude

Education system app (4)