Android page jump

 Knowledge:

Method 1 to create an Activity step:

  1. Create a layout file 2. Create a class and let this class inherit his parent class AppCompatActivity

   3. Rewrite the OnCreate method to load the layout file 4. Modify the configuration file

Method 2: Create an Activity

Right-click on the package name of the selected program and select the [New]à[Activity]à[Empty Activity] option, fill in the Activity information, and complete the creation.

Configure Activity

In an Android program, creating an Activity can be achieved by inheriting an Activity from a Java class. When creating an Activity in this way, you need to configure Acitivity in the <application> tag of the manifest file

<activity 

       android:name="cn.itcast.activitybasic.SecondActivity" />

If the package where the Activity is located is the same as the package name specified by the package attribute in the <manifest></manifest> tag of the AndroidManifest.xml file, the value of the android:name attribute can be directly set to ".Activity name". Take SecondActivity as an example , the sample code is as follows

<activity

        android:name=".SecondActivity">

</activity>

When the Android program runs, the system first looks for the MainActivity registered in the AndroidManifest.xml file, and then finds the OnCreate() method in the Activity, and loads the mian_activity layout file through the setContentView() method in this method.

 

It should be noted that if the <intent-filter> tag in an Activity tag of the AndroidMainifest.xml file is added:

<category android:name="android.intent.category.LAUNCHER" />, then the program will first look for the Activity in the AndroidManifest.xml file when the program runs, and the interface corresponding to the Activity is the first page displayed after the program runs.

Realize the jump of 3 pages

First create an Activity, which can be created manually, but it must be completed in 4 steps, or you can create an empty Activity directly by view creation. It has its own layout file and does not need to go through a series of settings.

  SecondActivity:

package cn.itcast.activitylifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

public class SecondActivityy extends AppCompatActivity {

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

ThirdActivity:

package cn.itcast.activitylifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class Thirdactivity extends AppCompatActivity {

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

MainActivity: Added the method of using intent: jumping to the page

package cn.itcast.activitylifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
   public void OnClick1(View view) {
       Intent intent = new Intent(MainActivity.this, SecondActivityy.class);
       startActivity(intent);//开始activity,跳转到下一个页面
   }
   public void OnClick2(View view){
           Intent intent = new Intent(MainActivity.this, Thirdactivity.class);
           startActivity(intent);
       }
    public void OnClick3(View view){
        finish();//退出
    }
   }

SecondActivity layout file: add one to activity_example1, edit box control

<?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:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </EditText>

</androidx.constraintlayout.widget.ConstraintLayout>

The layout file of ThirdActivity: activity_example2, add one, edit box control

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Thirdactivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </EditText>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity layout file: mian_activity layout file: set the layout of the initial page, add related controls

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="欢迎访问主页面!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转到第二个页面"
        android:onClick="OnClick1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转到第三个页面"
        android:onClick="OnClick2"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="关闭"
        android:onClick="OnClick3"/>

</LinearLayout>

Global description file: AndroidManifest.xml, if you create an Activity manually, you need to put:

<activity android:name=".SecondActivityy"></activity>
<activity android:name=".Thirdactivity"></activity>

Add to AndroidManifest.xml:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SecondActivityy"></activity>
        <activity android:name=".Thirdactivity"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

result:

Click the Jump to Second Page button:

 

 Click the jump to the third page button:

Click the close button to close the Activity

Return to the emulator main page 

 

Guess you like

Origin blog.csdn.net/dengfengling999/article/details/123683186