How to implement login jump with Android Studio

foreword

I wrote this project a long time ago, and now I am posting it for your reference. There may be some non-standard problems, if there is something wrong, welcome to criticize and correct. First, you need to install and configure the Java development environment, and choose any Android development tool for programming. It is recommended to download and install Android Studio software for program development. Before starting Android programming development, you need to have a certain understanding and mastery of Java basics.

1. Basic requirements

Implement a simple user login interface with the following functions:

1. By default, user information is not stored, and passwords are hidden by default.

2. You can remember the password and display the password by checking the box, and click to log in to realize the page jump to the main page.

3. Enter the main interface through "Login", exit the APP through "Exit", and return to the login page through "Return" on the main interface

4. The login interface displays the logged-in user name and the current user's login time

2. Key code analysis

1. When performing login redirection, manifest.xml must write the new activity name, otherwise the redirection cannot be realized

<activity android:name=".WelcomeActivity"/>

2. Obtain the relevant parameters (login name and password) of whether to remember the current user from SharedPreferences, set the account number and password to the text edit box, and check Remember the current user name and password

//从SharedPreferences中获取是否记住密码的参数
        final SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(this);
        boolean isRemember = preference.getBoolean("remember_pwd", false);
        //设置账号与密码到文本框,并勾选记住密码
        if (isRemember) {
            username.setText(preference.getString("name", ""));
            password.setText(preference.getString("password", ""));
            remember_pwd.setChecked(true);
        }

3. Set the user name and password verification; if the verification is successful and remember the password is checked, the password and user name will be saved, if not checked, the saved data will be cleared after successful login

 String inputName = username.getText().toString();
                String pwd = password.getText().toString();
                //进行登录用户名和密码校验
                if (inputName.equals("老王") && pwd.equals("123456789")) {
                    SharedPreferences.Editor editor = preference.edit();
                    if (remember_pwd.isChecked()) {//记住账号与密码
                        editor.putBoolean("remember_pwd", true);
                        editor.putString("name", inputName);
                        editor.putString("password", pwd);
                    } else {//清空数据
                        editor.clear();
                    }
                    editor.apply();

4. Jump to the main interface WelcomeActivity.java

 WelcomeActivity.actionStart(MainActivity.this, inputName, pwd);

5. Whether to display the password, the default is not to check the display password

@Override
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
    //如果选中,显示密码
      editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
           } 
    else {
    //否则隐藏密码
    editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
          }
       }

6. Log out

cancel.setOnClickListener(new View.OnClickListener() {
                                      @Override
                                      public void onClick(View v) {
                                          finish();
                                      }
                                  });

7. After successful login, the user name will be displayed

//登录成功后,用户名显示
    private void showWelcome() {
        Bundle bundle = getIntent().getExtras();
        String name = bundle.getString("username");
        myWelcome.setText("\n" + name + " 您好!\n    欢迎光临");
    }

 8. Get the current login time

private void showTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());
        //获取当前时间
        String str = formatter.format(curDate);
        myTime.setText("您的登录时间为:"+str);
    }

3. Page code display

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/b03533fa828ba61e560f92ebd1da230f324e5901"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="75dp"
        android:layout_marginEnd="32dp"
        android:hint="请输入用户名"
        app:layout_constraintBottom_toTopOf="@+id/editText2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        android:maxLines="1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.972" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="75dp"
        android:layout_marginEnd="32dp"
        android:hint="请输入密码"
        android:inputType="textPassword"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        android:maxLines="1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.546" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:text="用户登录"
        android:textSize="28sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/editText1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.89" />

    <Button
        android:id="@+id/button_login"
        style="@style/AlertDialog.AppCompat.Light"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="32dp"
        android:layout_marginRight="30dp"
        android:backgroundTint="@android:color/holo_blue_dark"
        android:text="登录"
        app:layout_constraintEnd_toStartOf="@+id/button_quit"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />

    <Button
        android:id="@+id/button_quit"
        style="@style/AlertDialog.AppCompat.Light"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginStart="30dp"
        android:layout_marginTop="76dp"
        android:layout_marginEnd="30dp"
        android:text="退出"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示密码"
        app:layout_constraintStart_toStartOf="@+id/button_login"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.3"
        app:layout_constraintStart_toStartOf="@+id/checkBox1"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:text="用户:"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/editText2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/editText1"
        app:layout_constraintVertical_bias="0.37" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:text="密码:"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/editText2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.718" />


</androidx.constraintlayout.widget.ConstraintLayout>

welcome.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="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="网上购书系统"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="@dimen/abc_text_size_headline_material"
        android:textStyle="bold"
        android:textColor="@android:color/holo_blue_dark" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="\n您好!\n欢迎光临"
        android:id="@+id/myLabelWelcome"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:textSize="@dimen/abc_text_size_large_material"
        android:textColor="@android:color/holo_red_dark" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/androidwelcomer"
        android:layout_below="@+id/myLabelWelcome"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/myLabelTime"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:textSize="@dimen/abc_text_size_medium_material" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="返      回"
        android:id="@+id/myButtonBack"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:textSize="@dimen/abc_text_size_large_material"
        android:onClick="onBackClick" />
</RelativeLayout>

5. Results display

1. The user login interface is as follows:

2. By default, user information is not stored, and passwords are hidden by default: 

 3. You can check the box to remember the password, display the password, and prompt whether the account password is correct or incorrect:

 4. After clicking Login, the page jumps to the main interface, and the main interface displays the current login time and user:

5. Project comprehensive demonstration 

Source code download: Android development practice login interface jump-Android Documentation Resources-CSDN Download 

Guess you like

Origin blog.csdn.net/qq_53860947/article/details/125852035