Android study notes (3): Add new activities and buttons

        As mentioned before, activities are similar to interfaces. For example, we can jump from a WeChat main interface to a chat interface. The way to achieve this is to jump from one activity to another activity. Now let's add a new activity and jump through a button.

1. Automatically create activities

        Android studio can automatically create an activity. Right-click in the app/src/main/java/com.example.myapplication/ directory->New->Activity->Empty Views Activity to create a new empty activity, as shown in the figure below

As you can see from the picture above, you need to set the activity name (activity name), Layout name (layout name), package name (package name), and the compilation language java. Default settings, finish

As you can see, two files are automatically generated, a java file and an xml layout file. You can click on the file to see the content, and try to modify the content according to the knowledge you learned before, such as adding a TextView to the layout.

        In addition, open the AndroidManifest.xml file, you will find that the content of this file has also been modified, and the following code has been added

<activity
    android:name=".MainActivity2"
    android:exported="false" />

I mentioned before that the AndroidManifest.xml file is similar to a registry, and all activities need to be registered in this file before they can be used. Among them, name refers to the activity class, exported is ignored and can be deleted. Therefore, we know that to create a new activity, we need to create a new activity class, add an xml layout file, and modify the ActivityManifest.xml file. You may wish to create an empty activity manually to further deepen your understanding.

        Now that a new activity has been added, how do we use it. We can jump from one activity to another with a button.

2. Add a Button button

        We use buttons to jump from the activity_main interface (MainActivity) to the activity_main2 interface (MainActivity2). Then we need to add a button layout to the activity_main.xml file. The code is as follows

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Button"/>

</LinearLayout>

<Button ... /> The syntax is to add a button layout. I think you can easily understand the syntax through the previous study. In the strings.xml file, I set Button = go to activity2 . You can see the layout by clicking Split in the upper right corner, as shown below

 3. Implement button logic and jump interface

        Open the MainActivity.java file and modify the code as follows

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = findViewById(R.id.text);
        textView.setText("Hello Android!");

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, MainActivity2.class);
                startActivity(intent);
            }
        });
    }
}

        First, create a Button class and reference the button layout through findViewById(R.id.button). Then set a listening event for the button, where new View.OnClickListener() {} is an anonymous class. The function of the anonymous class is that when this class is only used in one place and is no longer used in other places, you can use the anonymous class. No need to Write an additional class. Override the onClick function in an anonymous class.

        The Intent class can now be simply understood as an object that transmits information. Set the context through intent and pass in MainActivity.this, MainActivity2.class in the setClass function. It can be simply understood as jumping from the MainActivity activity to the MainActivity2 activity. Finally, call the startActivity function to start the activity. In this way, you can jump from the activity_main interface to the activity_main2 interface by clicking the button. Remember to publish it to your mobile phone first.

4. APP main interface

        You may be curious, I have created multiple interfaces, so how does the system analyze which activity is the first one displayed when opening the APP. Then open the ActivityManifest.xml file. It is the file for registering activities. It must know which is the first activity.

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity2" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

        Careful friends may have noticed when creating a new activity that the contents of the two activities are different. The intent-filter module has been added to MainActivity, and the content inside it is the key to distinguishing which activity is the first activity. You can try to put the content in MainActivity2 and open the APP on your phone to see if the first interface is the layout in activity_main2.

5. Summary

        Through studying this article, we learned to create a new activity and a button, and jump from one activity to another through the button. Finally, we analyze how the APP determines the first activity.

Guess you like

Origin blog.csdn.net/CJ_study/article/details/133847081