[Android Study Notes] Android Studio Lesson 0--MainActivity

One after another, I studied myself for a while. Overall, a learning direction and practice method have been determined:

I found a very simple method. At this stage, I am learning, and there are not too many complicated functions. The main thing is to be familiar with the common controls, layout, and event monitoring and processing in Android development. Without any foundation, plus work and a heart in the distance, self-learning can be said to be slower than a snail. But take it slowly, it doesn't matter at least I haven't completely discarded it yet. Without further ado, let's focus.

The main idea is to create a most basic Activity in Lesson 0 (it is understood as an interface, which is called "interface" below). Create buttons for each study and practice in the basic interface (MainActivity), and click the button to enter the interface for each study and practice, as shown in the following figure:


Paste the code below:

package com.example.urien.secondapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    //1. Declare the control
    private Button bt_Lesson1;
    private Button bt_lesson2;
    private Button bt_lesson3;
    private Button bt_lesson4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {//onCreate is used to create an activity
        super.onCreate (savedInstanceState);
        //setContentView method is to display the content defined in activity_main in MainActiviy
        setContentView(R.layout.activity_main);

        //2. Find the control
        bt_Lesson1 = findViewById(R.id.bt_lesson1);
        bt_lesson2 = findViewById(R.id.bt_lesson2);
        bt_lesson3 = findViewById(R.id.bt_lesson3);
        bt_lesson4 = findViewById(R.id.bt_lesson4);
        //4. Generate a listener object
        setListener();
    }

    private void setListener(){
        OnClick onclick = new OnClick();
        bt_Lesson1.setOnClickListener(onclick);
        bt_lesson2.setOnClickListener(onclick);
        bt_lesson3.setOnClickListener(onclick);
        bt_lesson4.setOnClickListener(onclick);
    }

    //3. Define a class that implements the listener interface
    private class OnClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Intent intent = null;
            switch(v.getId()){//Jump to the corresponding avtivyty implementation method
                case R.id.bt_lesson1:
                    intent = new Intent(MainActivity.this,Lesson1_Activity.class);
                    break;
                case R.id.bt_lesson2:
                    intent = new Intent(MainActivity.this,Lesson2_Activity.class);
                    break;
                case R.id.bt_lesson3:
                    intent = new Intent(MainActivity.this,Lesson3_Activity.class);
                    break;
                case R.id.bt_lesson4:
                    intent = new Intent(MainActivity.this,Lesson4_Activity.class);
                    break;
            }
            startActivity(intent);
            MainActivity.this.finish();
        }
    }
}

There are not many main knowledge points here. There are four main steps for a button to start from scratch to event monitoring and processing, and the code is also marked with comments.

        * 1. Get the object representing the control
        * 2. Define a class to implement the listener interface
        * 3. Generate the listener object

        * 4. Bind the listener object to the control

For the above code, I bound all key monitoring to a listener for convenience, so that it is convenient to add and modify the code in the future. It can also be bound in a separate way, as follows:

    //1. Declare the control
    private Button bt_Lesson1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {//onCreate is used to create an activity
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);//setContentView method is to display the content defined in activity_main in MainActiviy

        //2. Find the control
        bt_Lesson1 = findViewById(R.id.bt_lesson1);
        //4. Generate a listener object
        bt_Lesson1.setOnClickListener(bt_lesson1_listener);
    }

    //3. Define a class that implements the listener interface
    Button.OnClickListener bt_lesson1_listener = new Button.OnClickListener(){
        @Override
        public void onClick(View v) {
            //The following three lines of code are the implementation method of jumping avtivyty
            Intent intent = new Intent(MainActivity.this,Lesson1_Activity.class);
            startActivity(intent);
            MainActivity.this.finish();
        }
    };
In this case, one is fine, but it will be troublesome to write more. After all, there are a lot of repetitive codes. Another advantage is that you can set up different types of listeners. For example, if you learn CheckBox later, there are multiple listeners. It is best to bind together, the code looks comfortable.


By Urien May 6, 2018 14:53:26

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325953324&siteId=291194637