What is the value of R.id.ImageButton?

I have sixteen image buttons, so I am creating an array of image buttons. I would like to initialize them with a for loop, I would not want to do this one by one, although I do not know if this is possible. For a single button image, this will be something like:

imgbtn[0] = (ImageButton)findViewById(R.id.imgButton1);

I see that the R.id.smth part is an integer. Does it say somewhere where this integer value starts with imgButtons? So that I can do something like this:

int value = R.id.imgButton1;
for(int i = 0; i < imgbtn.length; i++)
{
     imgbtn[i] = (ImageButton)findViewById(value);
     value++;   //or something along these lines
}
+4
source share
2 answers
Identifiers

, , . getIdentifier()

int value = 1;
for(int i = 0; i < imgbtn.length; i++){
    int resId = getResources().getIdentifier("imgButton" + value, "id", this.getPackageName());
    imgbtn[0] = (ImageButton) findViewById(resId);
    value++;
}

GetResources(). getIdentifier (tag, type, package) int, (R) . , .

+7

, . , .

gen/R.id , - :

public static final class id {
    public static final int action_settings=0x7f080001;
    public static final int dimensionPlusView=0x7f080000;
}
public static final class layout {
    public static final int activity_main=0x7f030000;
}
public static final class menu {
    public static final int main=0x7f070000;
}
public static final class string {
    public static final int action_settings=0x7f050001;
    public static final int app_name=0x7f050000;
    public static final int photo_credit=0x7f050002;
}
+3

Source: https://habr.com/ru/post/1539049/


All Articles