Android Button button click event listener to achieve several ways

How projects usually work together to develop personal, but everyone has everyone's coding style, do not pay attention to the code specifications are usually digging predecessors, descendants of mining pit, reduce project development efficiency and increase maintenance difficulty, to share here several Android implementations button click event listening.

Method 1 : directly in Activity in View.OnClickListener interface rewrite onClick method, providing all buttons entrance event listeners. This is the actual project in the most common way, especially if there are multiple pages of buttons more applicable.

package com.example.myapp;

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

public class MainActivity extends Activity implements View.OnClickListener {

private Button button1;
private Button button2;

protected void onCreate(Bundle savedInstanceState) {
button1 = findViewById(;
button2 = findViewById(;

public void onClick(View view) {
switch (view.getId()) {

private void onClickButton1(View view) {

private void onClickButton2(View view) {

Usually there will be more pages can be a button, you can get to the button click event Id occur by OnClick method View.getId (), and then make the appropriate treatment; not recommended to directly address the OnClick method, because the processing of multiple buttons logic written in the same method, the method of the body longer, reducing readability.

Second way: direct interface to achieve the listener when the button is registered listeners that the anonymous inner class manner. Page only when the more common single button.

register_btn.setOnClickListener ( new new View.OnClickListener () { 
@Override public void the onClick (View View) { IF (TextUtils. isEmpty ( userName_et.getText (). toString ()) || TextUtils. isEmpty ( pwd_et.getText (). toString ())) { Toast. makeText (RegisterActivity. the this, "username and password can not be empty" , Toast. LENGTH_LONG) .Show () ; return; } // Register } }) ;

familiar with functional programming, you can write directly lambda expressions, code more concise.

Three ways: a custom listener to achieve View.OnClickListener the interface, which is a similar way, a direct way in Activity in View.OnClickListener interface, so it is itself a listener. Here we must additionally implement a listener class.
class MyListener{View.OnClickListener the implements

@Override public void the onClick (View View) { // the processing logic } }

Finally do not forget custom registered in the listener Activity
register_btn.setOnClickListener ( new new MyListener ()) ;

Four ways: OnClick property XMl layout file directly in the Activity registration processing method, public methods when clicked event handling method Activity in need, will be called by the underlying reflective way. This method is well maintained, usually easy to find the button corresponding to the Click event handler method is not recommended
XML layout file: 
<ImageButton Android : layout_marginTop = "50dp" Android : layout_height = "49dp" Android : layout_width = "55dp" Android : layout_gravity = "Center" Android : the onClick = " the activate " /> the Activity write a corresponding processing method , access is public:

public void the activate (View V) {
the Intent intentToActivate = new new the Intent () ;
intentToActivate.setClass ( the this, LoginActivity. class) ;
startActivity (intentToActivate) ;


Guess you like