What are “tag” and “id” on Layouts?

后端 未结 3 566
执念已碎
执念已碎 2021-02-05 07:48

I know how the switch statement works but I don\'t know what this means (R.id.webbutton). Can anyone please explain what it is and also what is TAG? Is there any guide for the

3条回答
  •  终归单人心
    2021-02-05 07:55

    IDs and Tags

    IDs

    Views may have an integer id associated with them. These ids are typically assigned in the layout XML files, and are used to find specific views within the view tree. A common pattern is to:

    Define a Button in the layout file and assign it a unique ID.

    From the onCreate method of an Activity, find the Button

    Button myButton = (Button) findViewById(R.id.my_button);
    

    View IDs need not be unique throughout the tree, but it is good practice to ensure that they are at least unique within the part of the tree you are searching.

    Tags

    Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.

    Tags may be specified with character sequence values in layout XML as either a single tag using the android:tag attribute or multiple tags using the child element:

     
     
         
     
    

    Tags may also be specified with arbitrary objects from code using setTag(Object) or setTag(int, Object).

提交回复
热议问题