SoFunction
Updated on 2025-03-11

Three ways to implement Android click event onclick

The definition methods of onclick events are divided into three types, namely, specifying methods in XML; new OnClickListenner() in Actitivy; and implementing the OnClickListener interface.
The codes are as follows:
1. xml specifies the onclick event. This method is more suitable for the specified button, which can make the Java code relatively simplified:
In the xml file:

<span style="color:#333333;"><Button android:text="Button03"  
  android:  
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  <span style="color:#FF0000;">android:onClick="</span><span style="color:#ff0000;">Btn3OnClick</span><span style="color:#333333;">"</span>>  
</Button> </span> 

The red part specifies the method name of the response!
Methods defined in Activity:

public void Btn3OnClick(View view){  
  Intent intent = new Intent(, );  
  ("data", "mainActivity");  
  startActivity(intent);  
}  

2. Specify binding operations for the button in the onCreate method. In the following methods, if the execution method of the event is specified in the xml at the same time, the content in the xml will be executed first.

protected void onCreate(Bundle savedInstanceState) {  
  (savedInstanceState);  
  setContentView();  
  findViewById(.Button03).setOnClickListener(new OnClickListener(){  
    @Override  
    public void onClick(View v) {  
      Intent intent = new Intent(, );  
      ("data", "mainActivity");  
      startActivity(intent);       
    }      
  });  
}  

This method will make the code look more intuitive, but it is not efficient, especially when you write to getView in the adapter of the listView, multiple objects are new, occupying resources. The optimization version is written as follows, and the click object is proposed as follows:

protected void onCreate(Bundle savedInstanceState) {  
  (savedInstanceState);  
  setContentView();  
  findViewById(.Button03).setOnClickListener(onclick);  
}  
OnClickListener onclick = new OnClickListener(){  
  @Override  
  public void onClick(View v) {  
    Intent intent = new Intent(, );  
    ("data", "mainActivity");  
    startActivity(intent);   
  }  
}; 

3. Implement the OnClickListener interface. This method uses a lot of usage and can solve all onclick problems in the same activity.

public class mainActivity extends Activity implements OnClickListener{  
  protected void onCreate(Bundle savedInstanceState) {  
    (savedInstanceState);  
    setContentView();  
    findViewById(.Button02).setOnClickListener(this);  
    findViewById(.Button03).setOnClickListener(this);  
  }  
  public void onClick(View view) {  
    switch (()) {  
      case .Button03:  
        Intent intent = new Intent(, );  
        ("data", "mainActivity");  
        startActivity(intent);   
        break;       
      case .Button02:  
        Intent intent = new Intent(, );  
        ("data", "mainActivity");  
        startActivity(intent);   
        break;   
      default:  
        break;  
    }  
  }  
  ……  
}  

These three methods can realize the processing of click events, and you can analyze which one is more suitable according to the usage environment!