[Quick Solution] Android Button page jump function

Table of Contents

Let’s get started

Step 1: Create a new activity first

?edit

Step 2: Open the Java file MainActivity of the first page

Method 1: The direct jump function is as follows:

Method 2: Enter the password to perform the jump function as follows:

Things to note

Conclusion


Let’s get started

The first step: Create a new activity first

It will automatically generate two files, one is MainActivity and the other is activity_main2.xml

Step 2: Open the Java file MainActivity of the first page

Write jump function in the page.

Method 1: The direct jump function is as follows:
//From this piece
package com.example.myapplication04;

import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
//Use your own project here

//Just copy the following
public class MainActivity extends AppCompatActivity {

    //Declare the control
    private android.widget.Button mBtnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Find the control
        mBtnLogin=findViewById(R.id.btn_login);
        //Realize jump and jump directly...Method 1
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
              public void onClick(View view) {
                  Intent intent = null;
                  //Jump from MainActivity page to FunctionActivity page
                  intent = new Intent(MainActivity.this,MainActivity2.class);
                  startActivity(intent);
              }
            
        });
    }
}
Method 2: Enter the password to perform the jump function as follows:
package com.example.myapplication04;

import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;




public class MainActivity extends AppCompatActivity {

    //Declare the control
    private android.widget.Button mBtnLogin;
    private EditText mEtUser;
    private EditText mEtPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Find the control
        mBtnLogin=findViewById(R.id.btn_login);
        mEtUser=findViewById(R.id.et_1);
        mEtPassword=findViewById(R.id.et_2);
        //Realize jump and jump directly...Method 1
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
// public void onClick(View view) {
// Intent intent = null;
// //Jump from MainActivity page to FunctionActivity page
// intent = new Intent(MainActivity.this,MainActivity2.class);
// startActivity(intent);
// }
            public void onClick(View view){
                //Need to get the entered username and password
                String username=mEtUser.getText().toString();
                String password=mEtPassword.getText().toString();
                Intent intent = null;

                //Assume that the correct account number and password are lsl 123456 respectively
                if(username.equals("lsl") & amp; & amp;password.equals("123456")){
                    //If correct, jump
                    intent=new Intent(MainActivity.this,MainActivity2.class);
                    startActivity(intent);
                }
                else{
                    //Incorrect, login failure toast pops up
                }
            }
        });
    }
}

Let’s explain the second method above (you can skip this if you understand)

package com.example.myapplication04;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    //Declare the control
    private Button mBtnLogin; // Login button
    private EditText mEtUser; // Username input box
    private EditText mEtPassword; // Password input box

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // find the control
        mBtnLogin = findViewById(R.id.btn_login); // Find the login button based on id
        mEtUser = findViewById(R.id.et_1); // Find the username input box based on id
        mEtPassword = findViewById(R.id.et_2); // Find the password input box based on id

        //Set the click event of the login button
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Need to get the entered username and password
                String username = mEtUser.getText().toString(); // Get the text content in the username input box
                String password = mEtPassword.getText().toString(); // Get the text content in the password input box
                Intent intent;

                // Assume that the correct account number and password are "lsl" and "123456" respectively
                if (username.equals("lsl") & amp; & amp; password.equals("123456")) {
                    // If the username and password are correct, jump to the MainActivity2 page
                    intent = new Intent(MainActivity.this, MainActivity2.class);
                    startActivity(intent);
                } else {
                    // If the username and password are incorrect, you can handle it here, for example, a login failure prompt will pop up.
                    // You can use Toast or a custom dialog box to display login failure information
                }
            }
        });

    }
}

The above code is a simple Android application, which mainly implements a login interface. It contains the following sections:

  1. Import the required classes and packages.
  2. The class that declares the activity (Activity) and inherits from the AppCompatActivity class.
  3. In the onCreate() method, the layout file and initialization control are set.
  4. A click event listener (OnClickListener) is set for the button (mBtnLogin), and the corresponding logic is executed when the button is clicked.
  5. In the click event, obtain the text content in the username and password input boxes and make a judgment. If the username and password match, jump to the MainActivity2 page, otherwise the login failure can be processed.

Through the above two steps, a simple jump function can be implemented.

Points to note

But what we need to pay attention to is that the id of the button, that is, the name corresponding to the button, must be the same as what you defined.

Here I will provide all the xml files where the id codes corresponding to my buttons are located.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="@drawable/bk4"
    tools:context=".MainActivity">

    <TextView
        android:id="@ + id/tv_1"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:gravity="center"
        android:text="Jump page explanation"
        android:textColor="#140902"
        android:textSize="40dp"
        android:layout_marginTop="100dp"
        android:textStyle="bold"
        />
    <EditText
        android:id="@ + id/et_1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textColor="#00FFA1"
        android:textSize="16sp"
        android:hint="username:"
        android:maxLines="1"
        android:padding="10dp"
        android:layout_marginTop="70dp"
        android:background="@drawable/bg_transparent"
        />


    <EditText
        android:id="@ + id/et_2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textColor="#00FFA1"
        android:textSize="16sp"
        android:hint="password:"
        android:maxLines="1"
        android:padding="10dp"
        android:inputType="textPassword"
        android:background="@drawable/bg_transparent"
        android:layout_marginTop="25dp"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        >
        <android.widget.Button
            android:id="@ + id/btn_login"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:textSize="18sp"

            android:layout_gravity="center"
            android:text="Login"
            android:background="@drawable/but_1"
            android:textAllCaps="false"
            />
        <android.widget.Button
            android:id="@ + id/btn_Join"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:layout_gravity="center"
            android:text="Join"
            android:layout_marginLeft="15dp"
            android:background="@drawable/but_1"
            android:textAllCaps="false"
            />

    </LinearLayout>


</LinearLayout>

Conclusion

Today’s sharing is over. Follow the Douyin account “Xiao Guang is working hard” to share interesting codes every day.

Click on the personal business card below to make communication more convenient~ (Welcome to the blogger’s homepage to join our CodeCrafters alliance to communicate and learn together) strong>↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 57269 people are learning the system

syntaxbug.com © 2021 All Rights Reserved.