Private button abtn = NULL; // declare initialization in the class
1. Click the common button to add a new listener for each control. It is convenient and convenient to use the keyword new.
aBtn = (Button) findViewById(R.id.aBtn);aBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub}});
2. Implement an independent click listener class and may be used multiple times
aBtn = (Button) findViewById(R.id.aBtn);aBtn.setOnClickListener(new MyListener(){});public class MyListener implements OnClickListener{public void onClick(View arg0) {//TODO}}
Or only new once, used multiple times; for example, the refresh efficiency can be significantly improved when used in listitem
aBtn = (Button) findViewById(R.id.aBtn);aBtn.setOnClickListener(mListener);OnClickListener mListener = new OnClickListener() {@Overridepublic void onClick(View v) {//TODO}};
3. The interface can be implemented when many components on the Interface need to execute click events.
For example, the implements onclicklistener interface of the activity class
Oncreate ()
- Findviewbyid (R. Id. abtn). setonclicklistener (this );
- Findviewbyid (R. Id. backbtn). setonclicklistener (this );
The onclick method needs to be reloaded in the class, and click response is implemented by judging the ID here:
- @ Override
- Public void onclick (view v ){
- // Todo auto-generated method stub
- Switch (V. GETID ()){
- Case R. Id. abtn:
- Break;
- Case R. Id. backbtn:
- Finish ();
- Break;
- }
- }