Android studio button click page jump

(1) First create a page to be redirected, that is, a new page, which is redirected after clicking.

Steps: app--->src-->main-->res-->layout (right click)-->New-->Activity-->Empty Activity

 After creation, a java file with the same name will be generated at this time.

The initial interface code is as follows, and the interface is shown in the back, for reference only.

<?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:gravity="center_horizontal"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"/>
        <EditText
            android:id="@+id/edit1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="____________"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"/>
        <EditText
            android:id="@+id/edit2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="____________"
            android:password="true"/>
    </LinearLayout>
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"/>
</LinearLayout>

The jump interface code, the same interface is shown in the back.

<?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:gravity="center"
    android:background="@drawable/bg">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        android:textColor="#DDE0E4"
        android:text="欢迎进入智慧农业系统"
        />
</LinearLayout>

Since the page jump does not change, what changes is the main interface. Click the button on the main interface to make the page change, so you only need to write

MainActivity.java file.

Note: Different file names and package names are different. The following is my personal code for reference only.

package com.example.signin;

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;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button btn;
    private EditText edit1;
    private EditText edit2;

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

        initView();
    }

    private void initView() {
        btn = (Button) findViewById(R.id.btn);
        edit1 = (EditText) findViewById(R.id.edit1);
        edit2 = (EditText) findViewById(R.id.edit2);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(edit1.getText().toString().equals("aaa") &&
                   edit2.getText().toString().equals("aaa")){
                    Intent intent = new Intent(MainActivity.this,nextActive.class);
                    startActivity(intent);
                }
                else {
                    Toast.makeText(MainActivity.this,"输入有误,请重新输 
                     入",Toast.LENGTH_LONG).show();
                }
            }
        });
    }

}

The following is the login jump page I completed, for reference only.

Initial interface:

After entering the correct user name and password, jump to the following interface, for reference only.

 If the input is not correct, there will be a text prompt:

Guess you like

Origin blog.csdn.net/qq_69424518/article/details/127203827