On the Internet, I encountered two interesting constructions, and although I use only the first, I still took an interest up, which one is better to use in my projects? I admit that the question is banal, but I cannot ask;) So, the first construction:

private OnClickListener myListener = new OnClickListener() { public void onClick(View v) { // do something when the button is clicked } }; protected void onCreate(Bundle savedValues) { ... // Capture our button from layout Button button = (Button)findViewById(R.id.close); // Register the onClick listener with the implementation above button.setOnClickListener(myListener); ... } 

And the second no less interesting design:

 <Button android:layout_height="wrap_content" android:id="@+id/butHello" android:layout_width="wrap_content" android:text="Поздороваться" android:onClick="butHello_Click" /> 

Here we have defined the onClick event. < Is it considered to be generally from the " Best Practice " range?

After which they registered the method (to our .xml):

 public void butHello_Click(View v){ edtext.setText("Здравствуй, друг"); } 
  • I'm not an Android developer, but in the second version you just use XML and that's it) - kxko
  • @kxxko the following method is written to this xml;) we insert it into the MainActivity for example - Morozov
  • Well, yes, but what is the question actually? - kxko

2 answers 2

I would not recommend using the second option for the following reasons:

  1. It can only be used with an Activity. Fragments in the span.
  2. In any case, you don’t refuse to use the first option, but it’s better to do the same actions in one way, respectively, don’t refuse the first one - it’s better to abandon the second one.
  3. So you can only subscribe to onClick, the rest of the calbeks are in the span, so again for uniformity it is better to subscribe to onClick just like the others.
  4. If you use the second option, then when your code is taken by a person who does not use this method it will be very difficult for him to understand how control gets into a method that does not seem to be called anywhere. Yes, and you yourself after a while can forget where the method is used, and Android Studio will show that it is not used anywhere.
  5. From the previous paragraph, it also follows that during refactoring, you can rename the method and not notice that it was used in some xml, especially if it was used in several markup variants.

In general, my opinion: if you want fewer problems - do not use the second option.

    All three designs have a place to be. Which one to use? - This is a question of organizing the code, if you want to know in detail what this was done for. Syntactic sugar, convenience - call it what you want. To make your code more readable, or just some more convenient via XML (there are developers who come from other languages, they could use one of the options, and if it is available here, it will be easier to get used to the new tool).

    Your question has no definite answer. This is a subjective thing and everyone does as he sees fit.

    • one
      "All three constructions"?) Is this probably a typo?) There are two constructions here)) - Ksenia
    • four
    • one
      @Asgard, not, only two ways. All methods of program assignment work in the same way - you create a class that implements the interface, and then pass it to the setOnClickListener method. The differences are only visible, in spelling, but the principle is the same. - Yuriy SPb
    • one
      @YuriSPb not, three ways. Ways - all three work on the same principle. through XML in the same, the second and third methods also take place to be. Speaking from your position, he is the only one - this is an onclicliner. All three are states of one. - Silento
    • 2
      After the @pavlofff method setOnClickListener is not passed the class itself, but an instance of the class? Then what's the difference, an instance of an explicit or anonymous class? - Nick Volynkin