android, Tutorial 011-1, UI, ImageButton , different kind of way to call ClickListener



Solution 1:

   ImageButton ImageView01 = (ImageButton)findViewById(R.id.ImageView01);
   ImageView01.setOnClickListener(new OnClickListener() {
	 public void onClick(View v) {

	 }

   });

Solution 2:


// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};

protected void onCreate(Bundle savedValues) {
...
// Capture our button from layout
ImageButton ImageView01 = (ImageButton)findViewById(R.id.ImageView01);
// Register the onClick listener with the implementation above
ImageView01.setOnClickListener(mCorkyListener);
...
}

Solution 3:

public class ExampleActivity extends Activity implements OnClickListener {
    protected void onCreate(Bundle savedValues) {
        ...
        Button button = (Button)findViewById(R.id.corky);
        button.setOnClickListener(this);
    }

    // Implement the OnClickListener callback
    public void onClick(View v) {
      // do something when the button is clicked
    }
    ...
}

reference:

http://developer.android.com/guide/topics/ui/ui-events.html


Leave a Reply