What does View method getId() return?

前端 未结 1 1596
太阳男子
太阳男子 2021-02-14 16:48

What is the co-relation between View\'s getId() method and the id defined in the XML file? Is there any? My IDs look like this: \"field1\", \"field2\"... This digits at the end

相关标签:
1条回答
  • 2021-02-14 17:05

    What is the co-relation between View's getId() method and the id defined in the XML file?

    getId() returns the android:id value, or the value you set via setId().

    My IDs look like this: "field1", "field2"

    In your XML, they may look like @+id/field1 and @+id/field2. Those are allocating ID resources, which are turned into numbers at compile time. These are the numbers you refer to in Java code as R.id.field1 and R.id.field2.

    This digits at the end are very important for my application and I need to retrieve them. Is there any way to do it?

    Have a big switch statement comparing the ID to R.id.field1, etc. Or, set up a HashMap to map between the getId() values and your "very important" numbers. Or, find some other solution that does not involve magic names on your ID values.

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