How to handle a button being clicked in Android?
In Android there seems to be 3 common ways of handling button clicks, how much difference is there between the methods? And are any of them 'better' in some way?
The three methods I keep seeing are:
Anonymous class
Find the button by it's ID, then pass a new anonymous class to setOnClickListener
, e.g. in onCreate
findViewById(R.id.myButton).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// .. Whatever
}
});
Implement OnClickListener
Implement OnClickListener
and pass this
to setOnClickListener
, then use a switch statment bas开发者_如何学编程ed on the button ID, e.g. in onCreate
findViewById(R.id.myButton).setOnClickListener(this);
and implement onClick
like
public void onClick(View v) {
switch(v.getId()) {
case R.id.myButton:
// ... whatever ...
break;
}
}
Use onClick XML atribute
In the XML layout for your activity, instead of giving your button an ID, use onClick
like this:
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="buttonClicked"
android:text="Button" />
Then have a buttonClicked
method in your Acitiviy like this:
public void buttonClicked(View v) {
// ... whatever ...
}
At the moment I tend to use the XML attribute, but that's just because it involves the least amount of code. When should I use the other methods?
The first two are the classic approaches. Which one you prefer is more of a general Java question than an Android question. The third one was added later to make things easier.
Setting up a click listener on a button is very common task, but it requires quite a bit of boilerplate code. One way to reduce the amount of boilerplate is to share a single click listener between several buttons. While this technique reduces the number of classes, it still requires a fair amount of code and it still requires giving each button an id in your XML layout file. With Android 1.6, none of this is necessary. All you have to do is declare a public method in your Activity to handle the click (the method must have one View argument)
Source
I've really always seen it as preference. I'm not sure there's any performance advantage to either other than the last two methods may be slightly faster since they're not creating objects at runtime.
The first option isolates the code to the single button so it's very easy to debug since you know only that code will be executed when that button is clicked. However, many buttons can cause initialization methods to expand to large sizes.
The last two methods put all button handling in one place which can be convenient and cleaner at times, but with many buttons you have to decipher which button was tapped by the user via the v.getId()
method.
The last option allows you to easily specify specific methods for specific buttons so you can separate them out like that, but again you'll have many methods used for single purposes.
I've always used the inline method (anonymous class) for custom dialog windows that have buttons since it keeps the code with the rest of the dialog rather than in an activity or class. I just initialize the buttons of the custom dialog when I override the onCreateDialog
.
I implement the OnClickListener on the Activity if the buttons are on the main window.
精彩评论