How to implement button click event in android studio (three methods)

I'm a novice. I just learned programming not long ago. I taught myself completely. I read these online and summarized them myself. Please correct me if there are any mistakes.

Method 1: Add an onClick attribute to the button that requires a click event in the layout file. As shown below:

 Then add the implementation code in MainActivity.java, such as:

public void changeStr(View view) {
        textView.setText("按了第1个按钮。");

 This method is suitable for a single button, and I think this method seems to be easier to understand, and it is similar to other programming languages ​​​​for implementing button functions.

All code:

Method 2: When binding the control ID, create a View.OnClickListener() and pass in the setOnClickListener method. In fact, it is implemented using onclicklistener. I don’t understand this very well. This method does not require changing the layout file and is suitable for a single button. The code is written in oncreate.

findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                textView.setText("按了第2个按钮");
            }
        });

Method 1 and 2 routines:

Layout file:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="300dp"
        android:text="Hello World!"

        android:textSize="40dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="changeStr"
        android:text="onclick" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="listener" />

</FrameLayout>

 MainActivity.java

package com.example.button;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.DataOutputStream;

public class MainActivity extends AppCompatActivity  {
    private TextView textView;
    private Button button1;
    private Button button2;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1=(Button)findViewById(R.id.button1);
        textView=(TextView)findViewById(R.id.textView);

       findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                textView.setText("按了第2个按钮");
            }
        });
    }

    public void changeStr(View view) {
        textView.setText("按了第1个按钮。");
    }
}

 Method 3: Bind the id to each control, and then use swich to determine the id in the rewritten onClick to implement the code function. This method is suitable for multiple buttons , the code is simple and the logic is clear.

1. Bind the method to the control in oncreate:

        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);

Then press alt+enter on this to bring up the picture below. After pressing Enter in the second item, another window will pop up. Press Enter again. I only know this completion code, and I don’t know what’s going on.

2. Rewrite onclick.

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1:
                textView.setText("按了第1个按钮");
                break;
            case R.id.button2:
                textView.setText("按了第2个按钮");
                break;
            case R.id.button3:
                textView.setText("按了第3个按钮");
                break;
        }
    }

Method 3 routine:

Layout file:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="300dp"
        android:text="Hello World!"

        android:textSize="40dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       
        android:text="onclick" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="listener" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="重写onclick" />

</FrameLayout>

MainActivity.java

package com.example.button;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.DataOutputStream;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView textView;
    private Button button1;
    private Button button2;
    private Button button3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);       
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        textView=(TextView)findViewById(R.id.textView);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1:
                textView.setText("按了第1个按钮");
                break;
            case R.id.button2:
                textView.setText("按了第2个按钮");
                break;
            case R.id.button3:
                textView.setText("按了第3个按钮");
                break;
        }
    }
}

 

Guess you like

Origin blog.csdn.net/kim5659/article/details/126327208