Dynamic element Id to setId(int) - Android

前端 未结 3 418
囚心锁ツ
囚心锁ツ 2021-01-16 16:33

I saw the syntax definition of setId(int) and findViewByID(int).

But how we use findViewById(R.id.row1)

I tried using

相关标签:
3条回答
  • 2021-01-16 16:36

    You can define your Ids as resources and then use setId() of the view to set it.

    0 讨论(0)
  • 2021-01-16 16:43

    You can use setId(int) only for objects extending View class.

    0 讨论(0)
  • 2021-01-16 16:49

    It's not recommended to set your object id yourself , findViewById make access to your R class that contains pointers to your XML items id.

    when you define an id in your Xml Layout file like

    android:id="@+id/txtyourId"
    

    in compile time compiler create a reference to your XML Item layout in R class that you can access it from your java code by findViewById

    View YourItem = (View)this.findViewById(R.id.txtyourId);
    
    0 讨论(0)
提交回复
热议问题