Android dynamic setId to avoid duplicate IDS
When two controls with the same id are added to a layout, an error occurs when two controls with the same id are added through findVIewById.
The solution is as follows:
1. View
generateViewId()
Method to obtain an available id. Perform setId manually.
Api level 17 Required
The code of this function can be directly used below api level 17:
public static int generateViewId() { for (;;) { final int result = sNextGeneratedId.get(); // aapt-generated IDs have the high byte nonzero; clamp to the range under that. int newValue = result + 1; if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0. if (sNextGeneratedId.compareAndSet(result, newValue)) { return result; } }}
2. General methods:
Samples/ApiDemos/src/com/example/android/apis/RadioGroup1.java
Samples/ApiDemp/res/values/ids. xml
Declare an id item
Set the corresponding name through setId in java code
NewRadioButton. setId (R. id. snack );