SoFunction
Updated on 2025-03-04

Four click events for Android Button button

This article has shared four click events for Android Button button for your reference. The specific content is as follows

The first type:Internal class implementation

Set Button attribute first

<Button
 android:;
 android:layout_width="wrap_parent";
 android:layout_height="wrap_parent"
 android:text="Button"/>

2. Find the button

Button btn =(Button)findViewById(.button1)

3. Set a click event for Button

(new MyClickListener())//IncomingIt's the ClickListener parameter, so we have to define a parameter interface

4. Define a class to implement the interface type required by the button

public MianActivity extend Activity(){
...
...
private class MyClickListener()implent OnclickListener{
 //Called when the button is clicked public void Onclick (View v){
  // Write the click event method here  ("Clicked")
  }
}
 }

The second type:Using anonymous internal classes to implement

Set Button attribute first

<Button
 android:;
 android:layout_width="wrap_parent";
 android:layout_height="wrap_parent"
 android:text="Button"/>

2. Find the button

Button btn =(Button)findViewById(.button1);

3. Set a click event for Button

//Anonymous internal classpublic MianActivity extend Activity(){
...
...
(new OnClickListener(){
 public void Onclick (View v){
  // Write the click event method here  ("Clicked")

  }
} )
  };

The third type:Activity implements the OnclickListener interface suitable for multiple button situations

Set Button attribute first

<Button
 android:;
 android:layout_width="wrap_parent";
 android:layout_height="wrap_parent"
 android:text="Button"/>
<Button
 android:;
 android:layout_width="wrap_parent";
 android:layout_height="wrap_parent"
 android:text="Button 2"/>
 <Button
 android:;
 android:layout_width="wrap_parent";
 android:layout_height="wrap_parent"
 android:text="Button 3"/>

2. Find the button

Button btn =(Button)findViewById(.button1)
Button btn2 =(Button)findViewById(.button2)
Button btn3 =(Button)findViewById(.button3)

3. Set a click event for Button

public MianActivity extend Activity implement OnClickListener(){
  ...
  ...
  Button btn =(Button)findViewById(this);//this stands for MainActivity  Button btn2 =(Button)findViewById(this)
  Button btn3 =(Button)findViewById(this)

  public void Onclick (View v){
  //Determine which button is clicked specifically  switch(()){
  .button1:// means click the first button   TODO();//Implement specific methods   break;
  .button2:
   TODO();//Implement specific methods   break;
  .button3:
   TODO();//Implement specific methods   break;  
  default:
   break;
  }

  }
  private void TODO(){
   //Specific method  }
}

The fourth type:Declare onclick in xml

Set Button attribute first

<Button
 android:;
 android:layout_width="wrap_parent";
 android:layout_height="wrap_parent"
 android:text="Button"
 android:onClick="click"/>

2. Find the button

Button btn =(Button)findViewById(.button1)

3. Declare a method, the method name is the same as the Onclick attribute declared in the XML layout of the button you want to click.

public void **click**(View v){
 TODO();//Implement specific methods}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.