I usually implement click event bound to a certain visual element, like
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
Often I see examples where they use Activity-wide onclick (implementing View.OnClickListener) and then they do not create View.OnClickListener for each element but rather just pass this, like
public class MyClass extends Activity implements View.OnClickListener {
//...
someUIElement.setOnClickListener(this);
public void onClick(View view) {
//TODO implement this
}
}
When should I use such Activity-wide OnClick events? Are both ways the same
Hi Please check may be helpful
How to handle button clicks using the XML onClick within Fragments
http://developer.android.com/guide/topics/ui/ui-events.html
http://developer.android.com/reference/android/widget/Button.html