Three Implementation Methods of Android Click Event onclick

  • 2021-07-13 06:14:00
  • OfStack

The definition method of onclick event is divided into three kinds, which are the specified method in xml; In Actitivy, new gives one OnClickListenner (); Three ways to realize OnClickListener interface.
The codes are as follows:
1. xml specifies the onclick event, which is more suitable for the specified button and can simplify the java code by 1:
In the xml file:


<span style="color:#333333;"><Button android:text="Button03"  
  android:id="@+id/Button03"  
  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(mainActivity.this, fristActivity.class);  
  intent.putExtra("data", "mainActivity");  
  startActivity(intent);  
}  

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


protected void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.main);  
  findViewById(R.id.Button03).setOnClickListener(new OnClickListener(){  
    @Override  
    public void onClick(View v) {  
      Intent intent = new Intent(mainActivity.this, fristActivity.class);  
      intent.putExtra("data", "mainActivity");  
      startActivity(intent);       
    }      
  });  
}  

This method will make the code look intuitive, but it is not efficient, especially when getView is written in the adapter of listView, new produces multiple objects and occupies resources. The optimized version is written as follows, and the click object is put forward as follows:


protected void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.main);  
  findViewById(R.id.Button03).setOnClickListener(onclick);  
}  
OnClickListener onclick = new OnClickListener(){  
  @Override  
  public void onClick(View v) {  
    Intent intent = new Intent(mainActivity.this, fristActivity.class);  
    intent.putExtra("data", "mainActivity");  
    startActivity(intent);   
  }  
}; 

3. Realize the OnClickListener interface, which is used more, and can solve all onclick problems in the same activity.


public class mainActivity extends Activity implements OnClickListener{  
  protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  
    findViewById(R.id.Button02).setOnClickListener(this);  
    findViewById(R.id.Button03).setOnClickListener(this);  
  }  
  public void onClick(View view) {  
    switch (v.getId()) {  
      case R.id.Button03:  
        Intent intent = new Intent(mainActivity.this, fristActivity.class);  
        intent.putExtra("data", "mainActivity");  
        startActivity(intent);   
        break;       
      case R.id.Button02:  
        Intent intent = new Intent(mainActivity.this, loginActivity.class);  
        intent.putExtra("data", "mainActivity");  
        startActivity(intent);   
        break;   
      default:  
        break;  
    }  
  }  
   ...   
}  

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


Related articles: