Cast result findViewById android method

后端 未结 5 735
无人共我
无人共我 2020-12-03 12:46

Why do we always have to cast the value returned by the method findViewById(id) ? The method already returns a view, as I\'ve seen in google reference :

5条回答
  •  难免孤独
    2020-12-03 13:37

    Yes, findViewById() returns View. You can omit casting if you don't need to use the methods of any direct/indirect subclasses, that extends View, such as ImageView, LinearLayout, TextView etc.

    View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.)

    And of course you can downcast to any subclass of View

提交回复
热议问题