That Don Guy
Lurker
I have noticed three different ways of handling a Button click:
First, put the handling code into the setOnClickListener call:
Second, use OnClickListener with parameter "this", and handle the click in a separate onClick call:
Third, include android.onClick in the XML:
Are there any advantages in using one of these ways over the others?
-- Don
First, put the handling code into the setOnClickListener call:
Code:
<Button android:id="@+id/someButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/Click" />
-----
final Button someButton = (Button) findViewById(R.id.someButton);
someButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// handle someButton's click
}
});
Code:
<Button android:id="@+id/someButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/Click" />
-----
final Button someButton = (Button) findViewById(R.id.someButton);
someButton.setOnClickListener(this);
@Override
public void onClick(View v)
{
if (v == someButton)
{
// handle someButton's click
}
}
Code:
<Button android:id="@+id/someButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/Click"
android:onClick="someButton_Click" />
-----
public void someButton_Click(View v)
{
// handle someButton's click
}
-- Don