What is the difference between these two buttons? Android Programming

Ok ive just started programming some kind of android in Eclipse, and I'm a little overwhelmed about it. Here are two ways to program the buttons in this book, both of which work great, except that the first one seems simpler and shorter. Which is better and why?

View newButton = findViewById(R.id.main_new_button); newButton.setOnClickListener(this); Button newButton= (Button) this.findViewById(R.id.main_new_button); newButton.setOnClickListener(this); 
+4
source share
6 answers

Two forms are equivalent. In both cases, findViewById returns a View object, the only difference is that in the second version, explicit conversion is performed in Button , a subclass of View .

As you can see in the documentation , View is a superclass of TextView , and TextView is a superclass of Button .

Which one is better? it depends. If you need to use the functionality specific to Button , then the second method is preferable. On the other hand, if a View object exists, then use the first method.

+1
source

I like your way of thinking. In android, each widget is a view. I will tell you in a step -

1)

First of all, it is important that each button has a View, but not necessarily. Each species had to

  View newButton = findViewById(R.id.main_new_button); newButton.setOnClickListener(this); 

you use it, which does not mean that newButton is always Button.We can execute any View link (e.g. LinearLayout, ImageButton, etc.) in newButton

But in the second case

 Button newButton= (Button) this.findViewById(R.id.main_new_button); newButton.setOnClickListener(this); 

newButton should definitely be a Button if R.id.main_new_button is the button identifier in XML

2) -

As I said, we know that each Button is a View, but we don’t know what kind of button is. This will ask you to quit findViewByid. And your first case hinders, because each view has its own method, which cannot be used if we generalize this view. Therefore, we need to specifically use Button.

Hope you got it

+1
source

A button is a widget that extends a TextView. TextView extends the view. If you go to the Button class, you will come across more specific member functions, not the aforementioned superclasses. As for these specific methods ... you will need to go to the android.widget.Button class to learn this =). After that, all I see is three constructors (for explicit button declaration) - all other member functions are identical.

  public Button(android.content.Context context); public Button(android.content.Context context, android.util.AttributeSet attrs); public Button(android.content.Context context, android.util.AttributeSet attrs, int defStyle); 

But if you do not want to confuse yourself with the line when you return to this code after 3 months or other developers working with you, the second method is the right method:

 Button newButton= (Button) this.findViewById(R.id.main_new_button); newButton.setOnClickListener(this); 
0
source

If you want a button, you will need the last formation. The shaper is normal, but it is not an ideal method. Because the use of the view is not just for the button.

0
source

In the second case, you attached the result to the correct type. This allows you to access Button specific methods. The first returns only the View object, although this is a Button . Thus, you will not have access to Button specific functions.

0
source

The button created by View is a view, not a button widget. If you create a button from a view class, you cannot call Button Class functions. The Class and Button classes have a setOnClickListener function, but this View class function does not match the Button function.

0
source

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


All Articles