Dynamic element Id to setId(int) - Android

前端 未结 3 417
囚心锁ツ
囚心锁ツ 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: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);
    

提交回复
热议问题