How to handle a button pressed on Android?

In Android, there seem to be three common ways to handle button clicks, what's the difference between the methods? And are any of them β€œbetter” somehow?

The three methods that I see are as follows:

Anonymous class

Find the button by this identifier, then pass the new anonymous class to setOnClickListener , for example. in onCreate

 findViewById(R.id.myButton).setOnClickListener(new OnClickListener() { public void onClick(View v) { // .. Whatever } }); 

Deploy OnClickListener

Add OnClickListener and pass this to setOnClickListener , then use the switch setOnClickListener based on the button id, for example. in onCreate

 findViewById(R.id.myButton).setOnClickListener(this); 

and implement onClick as

 public void onClick(View v) { switch(v.getId()) { case R.id.myButton: // ... whatever ... break; } } 

Use atClick XML attribute

In the XML layout for your activity, instead of giving your button an identifier, use onClick as follows:

 <Button android:layout_width="fill_parent" android:layout_height="fill_parent" android:onClick="buttonClicked" android:text="Button" /> 

Then in your Acitiviy there is a buttonClicked method:

 public void buttonClicked(View v) { // ... whatever ... } 

I currently tend to use the XML attribute, but that is only because it includes the least amount of code. When should other methods be used?

+6
source share
2 answers

The first two are classic approaches. Which one do you prefer more a general Java question than an Android question. A third was added later to simplify the task.

Setting up a button click listener is a very common task, but it requires quite a bit of template code. One way to reduce the amount of the template is to share one click-listener between several buttons. Although this method reduces the number of classes, it still requires enough code, and you still need to enter the identifier in your XML layout file. With Android 1.6, none of these are 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)

A source

+4
source

I really always considered this a preference. I'm not sure if any performance advantage for other than the last two methods can be a little faster, since they do not create objects at runtime.

The first option isolates the code from one button, so it is very easy to debug, since you only know that the code will be executed when this button is pressed. However, many buttons can cause initialization methods to be expanded to large sizes.

The last two methods allow you to process all the buttons in one place, which can be convenient and clean at times, but with many buttons you have to decipher which button was pressed by the user using the v.getId() method.

The last parameter allows you to easily specify specific methods for specific buttons so that you can separate them, but again you will have many methods used for individual purposes.

I always used the built-in method (anonymous class) for custom dialogs that have buttons, since it stores code with the rest of the dialog, and not in the action or class. I just initialize the user dialog buttons when I override onCreateDialog .

I implement OnClickListener in Activity if the buttons are in the main window.

+1
source

Source: https://habr.com/ru/post/895171/


All Articles