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
source share