Account login case (for all kinds of username password login)

First, show the results of the project operation:
The initial login interface:

(1) Choose to remember the password:

Click login to jump to Activity 02 page:

(2) When logged in again:

Cancel remembering passwords:

Click login and jump to Activity 02 page. When you open the project again, it will return to the state of empty initial account and password.

1. Set up a project named Case_Login (the name of the project can be changed according to your own needs).
2. Analyzing the layout can be seen as a set of small layout in the big layout. activity_login.xml is the big layout. Under the login layout is a picture of a deer. include contains a layout above the login.
(1) The activity_login.xml code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_login"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/loginbg"
    tools:context="cn.edu.bzu.case_login.LoginActivity">

    <include layout="@layout/login_top"></include>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/deer"
        android:id="@+id/imageView"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="23dp" />
</RelativeLayout>

(2) The login_top.xml code is as follows:

Write code slices here.<?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:padding="@dimen/activity_horizontal_margin"
    android:background="@drawable/logintop_roundbg">

    <EditText
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/edit_text"
        android:drawableLeft="@drawable/icon_user"
        android:drawablePadding="10dp"
        android:ems="10"
        android:hint="@string/etName">
        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/etName"
        android:background="@android:drawable/edit_text"
        android:drawableLeft="@drawable/icon_pass"
        android:drawablePadding="10dp"
        android:ems="10"
        android:hint="@string/etPass"
        android:inputType="textPassword">
        <requestFocus />
    </EditText>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/etPassword">
        <CheckBox
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/password"
            android:layout_weight="1"
            android:id="@+id/ck_box"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/btn_select"
            android:text="@string/btnLogin"
            android:id="@+id/btn_login"/>
    </LinearLayout>
</RelativeLayout>

Because of the use of text in the layout file, we put it in res - > values - > strings. XML file. (How to quote it, in the above code, for example: android:text="@string/password")
The strings.xml code is as follows:

<resources>
    <string name="app_name">Case_Login</string>
    <string name="etName">Please enter your account number.</string>
    <string name="etPass">Please input a password</string>
    <string name="btnLogin">Sign in</string>
    <string name="password">Keep passwords in mind</string>
</resources>

In this way, we have finished the layout file of the login page, but the background does not look beautiful, so we need to modify the file, change the color background of button button, change the layout above the login page, and so on.
The documents used are as follows:

The btn_select.xml code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_shape" android:state_pressed="false"></item>
    <item android:drawable="@drawable/btn_shape_after" android:state_pressed="true"></item>
</selector>

The btn_shape.xml code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
   <solid android:color="#FF72CAE1"></solid>
    <corners android:radius="10dp"></corners>
</shape>

The btn_shape_after.xml code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#87cefa"></solid>
    <corners android:radius="10dp"></corners>
</shape>

The loginbg.xml code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
   <gradient
       android:startColor="#FFACDAE5"
       android:endColor="#FF72CAE1"
       android:angle="45"/>
</shape>

The logintop_roundbg.xml code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp"></corners>
    <solid android:color="#55FFFFFF"></solid>
</shape>

3. Start writing the interactive interface LoginActivity:

package cn.edu.bzu.case_login;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Map;

import cn.edu.bzu.utils.Utils;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private CheckBox ck_box;
    private EditText etName;
    private EditText etPassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        //Take out the number
        Map<String,String> userInfo= Utils.getUerInfo(this);
        if (userInfo!=null){
            etName.setText(userInfo.get("username"));
            etPassword.setText(userInfo.get("password"));
            SharedPreferences sp=this.getSharedPreferences("data",Context.MODE_PRIVATE);
         if(sp.getBoolean("check",false)){
                ck_box.setChecked(sp.getBoolean("check",true));
            }
        }
    }
    private void initView(){
        ck_box=(CheckBox)findViewById(R.id.ck_box);
        etName=(EditText)findViewById(R.id.etName);
        etPassword=(EditText)findViewById(R.id.etPassword);
        findViewById(R.id.btn_login).setOnClickListener(this);
    }
    public void onClick(View view){
        //When you click Login, get the account and password
        String username=etName.getText().toString().trim();
        String password=etPassword.getText().toString();
        //Verify that the account and password are empty
        if(TextUtils.isEmpty(username)){
            Toast.makeText(this,"Please enter your account",Toast.LENGTH_SHORT).show();
            return ;
        }
        if(TextUtils.isEmpty(password)){
            Toast.makeText(this,"Please input a password ",Toast.LENGTH_SHORT).show();
            return ;
        }
        //Judge user = admin, password = 123456
        if(username.equals("admin")&&password.equals("123456")){
            //Have you checked the password to remember?
            boolean CheckBoxLogin = ck_box.isChecked();
           if(ck_box.isChecked()){
               //Save user information
               boolean isSaveSuccess=Utils.saveUserInfo(this,username,password,CheckBoxLogin);
           }else{
               Utils.del(this);
           }
            Intent intent=new Intent(this,Activity02.class);
            startActivity(intent);
        }else {
            Toast.makeText(this,"Logon failure",Toast.LENGTH_SHORT).show();
        }
    }
}

There are a lot of codes here, because there is a need to open the file for reading and writing operations. After selecting the password to remember, you need to write the account and password to the file to solve the problem that the account and password will not disappear when you open it next time (read out from the file and display).
In order to make the code clear, store and read data in the tool class, so add a package cn.edu.bzu.utils, write a Utils class in this package for account and password storage and read.
The code for the Utils class is as follows:

package cn.edu.bzu.utils;

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

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Li Xiaoning on 2017/4/8.
 */

public class Utils {
    //Save the account and password to the data.xml file
    public static boolean saveUserInfo(Context context,String username,String password,boolean CheckBoxLogin){
        SharedPreferences sp=context.getSharedPreferences("data",Context.MODE_PRIVATE);
        SharedPreferences.Editor edit=sp.edit();  //Create an Editor object
        edit.putString("userName",username);
        edit.putString("pwd",password);
        edit.putBoolean("check",CheckBoxLogin);
        edit.commit();
        return true;
    }
    //Get the stored account and password from the data.xml file
    public static Map<String,String> getUerInfo(Context context){
        SharedPreferences sp=context.getSharedPreferences("data",Context.MODE_PRIVATE);
        String username=sp.getString("userName",null);
        String password=sp.getString("pwd",null);
        Map<String,String> userMap=new HashMap<String, String>();
        userMap.put("username",username);
        userMap.put("password",password);
        return userMap;
    }
    public static void del(Context context){
        SharedPreferences sp=context.getSharedPreferences("data",Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.clear();
        edit.putBoolean("check",false);
        edit.commit();
    }
}

4. Click on the login to jump to the Activity 02 interface to display Hello world
The layout file activity_02.xml of the Activity02 interface is as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_02"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.edu.bzu.case_login.Activity02">

    <TextView
        android:text="Hello world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/textView"
        android:textSize="30dp"/>
</RelativeLayout>

So our account is logged in.

Keywords: Android xml encoding Java

Added by madrazel on Fri, 12 Jul 2019 00:52:48 +0300