Android Mobile Application Development Tutorial③

  • This article is the third one, mainly explaining the understanding of Activity and the programming specification of Android. Explains the meaning of various codes in the xml file in the interface layout, and shows how to use java code to modify the content displayed in the xml interface. Finally, an example is used to show how to realize the jump of the app interface, and introduces two ways to create the activity interface.
  • This article is a note made during the learning process of the Bilibili tutorial Brain Academy Android tutorial!
  • Most of this article is the knowledge points selected from the video, and some of the text and a small part of the pictures are written by myself.
  • This article follows the previous article "Android Mobile Application Development Tutorial ②"
  • Next link: "Android Mobile Application Development Tutorial ④"

One: interface display

1.1: Understanding of Activity

Activity is an application component that provides a screen that the user can interact with to accomplish some task.

It is an interface. After we open the app, it is an activity. When clicking the button above to interact and jump to other interfaces, it jumps to another activity. It is a very important component in Android application development.

1.2: Android programming specification

Android uses xml tags to describe the application interface, and uses java code to write program logic.

Similar to HTML and JavaScript, xml is used to describe which controls are on the activity and where they are, while java code is used to interact with the interface and respond to user operations, which is to separate the front end from the back end.

benefit:

  • Using xml to describe the APP interface, you can easily display the preview interface effect on Android studio
  • A page layout can be reused by multiple codes, and the same piece of java code can also be adapted to different interface layouts

1.3: Interface Layout

Find the layout folder in the res folder, which contains the page layout xml file of activity_main. After opening, you can see the page layout (xml markup language) of the main page. If the drag and drop component interface is displayed, please select code in the upper right corner

The following will create an interface to explain the usefulness of various codes (please delete all the codes except the first line, and then copy the following codes in)

<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/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello world! "/>

</LinearLayout>

Every page must have a root layout, and the root layout in the above code is the LinearLayout linear layout.

The second line is to introduce the namespace of xml, which tells us which legal attributes can be used.

The three or four lines represent the width and height of our root node, and the following match-parent is the matching parent item, which is the same size as the screen.

The fifth line is to set the direction of the linear layout, and vertical means that the layout of the internal sub-controls is vertical.

The sixth line is to center the controls in the layout

TextView is a child control in a linear layout. The first line is the id of the control, which can be contacted with the back-end code, and the second and third lines are the size of the control. wrap_content represents changing the size with the internal text, and the fourth line is Content in TextView. for "hello world".

Two: logical processing

2.1: Write logic in java files

 If we want to change the text in the TextView in the page layout, how to write it

Enter the following code after line 12

 TextView tv=findViewById(R.id.tv);
        tv.setText("你好,世界");

In this way, after we publish and run, we will first find and execute our interface, and then find the tv control and change the text inside.

 Three: Jump to the app page

3.1: Complete page creation process

  1. Create an XML file in the layout directory
  2. Create the Java code corresponding to the XML file
  3. Register page configuration in AndroidManifest.xml

3.1.1: Create an xml file in the layout directory

Click to create a new layout xml file and name it activity_main2, and fill in the linear layout root layout (basically the same as written above)

<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">

    
</LinearLayout>

 Then insert the TextView component inside

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""/>

When filling in the text in the TextView, we can not directly fill in the Android label, we can write here

Fill it in

<string name="text2">Activity Main 2</string>

 This way we can directly reference text2 in the xml file

3.1.2: Create the Java code corresponding to the XML file

Create a java class in the java file package, as follows after creation

Then inherit the public class from the AppCompatActivity class, because it handles many compatibility issues.

Then override the onCreate method

Note: onCreate has two methods, please rewrite the second protected method.

And write mainactivity2 in manifest file

 

 3.2: Realize the jump of different pages

 3.2.1: Add component jump in the interface

Open the MainActivity interface file

Add a button component to LinearLayout

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

3.2.2: Write jump code in java code

Open the MainActivity class in the java file

Add some code in onCreate

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

The meaning of the code will be mentioned later. This completes the jump.

3.2.3: Testing

After clicking to jump

 3.3: Another simple way to create interfaces

Right click to create

 In this way, the corresponding xml files and java classes will be automatically created

It will also be added accordingly in the manifest file.

Guess you like

Origin blog.csdn.net/qq_64618483/article/details/129189379