03 Button Object Events

Posted by Ambush Commander on Thu, 23 May 2019 19:59:48 +0200

When the state of a window or control occurs, it automatically triggers the call of the corresponding event function. In Android, these events are managed and implemented by various kinds of listener classes, such as a button object that can set OnClickListener to set the listener triggered when it is clicked. The listener is an interface consisting of the abstraction function, and the specific event handling code is written in the implementation of the abstraction function.

The listeners that a button object can set are:

The use of the listener is basically to create a listener object and implement its image extraction function, and then set up the use of the listener object by the button object.

For example, there are two buttons in a window, and each button object clicks on different events, so each button object creates an OnClickListener object. The image extraction function onClick() is implemented when the object is created.

/* MyActivity.java */
package com.example.jk.listener;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MyActivity extends Activity {
    private Button btn1, btn2;
    private LinearLayout hlayout;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        btn1 = new Button(this);
        btn1.setText("btn1");
        btn2 = new Button(this);
        btn2.setText("btn2");

        hlayout = new LinearLayout(this);
        hlayout.setOrientation(LinearLayout.HORIZONTAL);

        hlayout.addView(btn1);
        hlayout.addView(btn2);

        setContentView(hlayout);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("btn1");
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("btn2");
            }
        });
    }
}

After compiling the executor, you can see the output information when you click the button in "Android Monitor":

You can also let the window class implement the OnClickListener interface. When you create a window object, you create an OnClickListener object. Let all button objects use the created listener object together.

/* MyActivity.java */
package com.example.jk.listener;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MyActivity extends Activity implements View.OnClickListener{ //Implementing the interface of the listener
    private Button btn1, btn2;
    private LinearLayout hlayout;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        btn1 = new Button(this);
        btn1.setText("btn1");
        btn2 = new Button(this);
        btn2.setText("btn2");

        hlayout = new LinearLayout(this);
        hlayout.setOrientation(LinearLayout.HORIZONTAL);

        hlayout.addView(btn1);
        hlayout.addView(btn2);

        setContentView(hlayout);

        //Set the button to use the listener object in the current window object
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }
    public void onClick(View v) {
        if (v == btn1)
            System.out.println("btn1");
        if (v == btn2)
            System.out.println("btn2");
    }
}

Topics: Android Java