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

后端 未结 3 562
执念已碎
执念已碎 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.

    <Button
       android:id="@+id/my_button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/my_button_text"/>
    

    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:

     <View ...
           android:tag="@string/mytag_value" />
     <View ...>
         <tag android:id="@+id/mytag"
              android:value="@string/mytag_value" />
     </View>
    

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

    0 讨论(0)
  • 2021-02-05 08:05

    Id is id of your xml's components [may be views like textview,edittext... or viewgroup like linearlayout ,relativelayout... or anything else] in xml simply you can get reference to them in java code by saying

    (R.id."id of your view in xml")

    but firstly you should use setContentView(R.layout."name of xml file in layout/res in your project") this xml file which you want to use it's components .

    TAG i use it when i want to show message in logcat [tool in eclipse you can watch your app messages when it is running] by saying String TAG= yourclassname.class.getsimpleName();

    and use it in Log.d(TAG,"any string here"+some variable in my class i want to know it's value in a particular time when app running );

    i hope that i made it clear to you .

    0 讨论(0)
  • 2021-02-05 08:12

    Start with the tutorials. (If you are so absolutely a beginner that you don't have a development environment set up yet, then start with Installing the SDK.)

    When you use the console log facility in Android, the first argument to the logging methods is a tag, which can be used to filter logcat output. A typical programming style is:

    public class Something { 
        private static final String TAG = "Something";
    
        public void aMethod() {
            Log.i(TAG, "Entered aMethod");
        }
        . . .
    }
    

    That's what TAG is.

    Resource IDs are explained in the tutorial. When you define a resource in XML, Android generates a class called R with nested classes for different kinds of resources (R.id, R.string, R.layout, etc.). Each of those nested classes has a constant for each resource of that type. R.id.webbutton might be generated from a layout file that has a button with attribute android:id="@+id/webbutton". This is all explained in the tutorials.

    0 讨论(0)
提交回复
热议问题