March 1st, 2011, 09:52 AM
|
#1 (permalink)
|
|
New Member
Thread Author (OP)
Join Date: Feb 2011
Posts: 7
Device(s):
Carrier: Not Provided
Thanks: 0
Thanked 1 Time in 1 Post
|
Preferred method for handling OnClick?
I have noticed three different ways of handling a Button click:
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
}
});
Second, use OnClickListener with parameter "this", and handle the click in a separate onClick 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(this);
@Override
public void onClick(View v)
{
if (v == someButton)
{
// handle someButton's click
}
}
Third, include android.onClick in the XML:
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
}
Are there any advantages in using one of these ways over the others?
-- Don
|
|
|