简体   繁体   中英

Assign view IDs in XML, access programmatically as integers

I want to be able to assign view IDs in XML, but access them programmatically as integers for the sake of iterating over them easily.

Here's the direction I was going which does not work:

values/value.xml:

<resources>
    <integer name="int1">101</integer>
    <integer name="int2">102</integer>
    ...
    <integer name="int10">110</integer>
</resources>

layout/fancy_dialog_fragment.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...etc.>

    <Button android:id="@integer/int1" ... />
    <Button android:id="@integer/int2" ... />
    ...
    <Button android:id="@integer/int10" ...etc. />


</LinearLayout>

FancyDialogFragment.java:

for (int i = 0; i < 10; i++ ) {
    button[i] = (Button) getDialog().findViewById(100+i);
    button[i].setText("foo");
}

How about this?

This goes into res/values/arrays.xml (you can call it whatever you want of course):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="myIds">
        <item>int1</item>
        <item>int2</item>
        <item>int3</item>
        <item>int4</item>
        <item>int5</item>
        <item>int6</item>
        <item>int7</item>
        <item>int8</item>
        <item>int9</item>
        <item>int10</item>
    </string-array>
</resources>

That's how the layout looks like:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...etc.>
    <Button android:id="@+id/int1" .../>
    <Button android:id="@+id/int2" .../>
    <Button android:id="@+id/int3" .../>
    <Button android:id="@+id/int4" .../>
    <Button android:id="@+id/int5" .../>
    <Button android:id="@+id/int6" .../>
    <Button android:id="@+id/int7" .../>
    <Button android:id="@+id/int8" .../>
    <Button android:id="@+id/int9" .../>
    <Button android:id="@+id/int10" .../>
</LinearLayout>

And there's the code:

Resources res = getResources();
String packageName = getPackageName();
String[] ids = res.getStringArray(R.array.myIds);
for (String id : ids) {
    int idInt = res.getIdentifier(id, "id", packageName);
    Button button = (Button) findViewById(idInt);
    button.setText("foo");
}

Create the file res/values/ids.xml and declare the ids there:

<resources>
    <item name="id_1" type="id"/>
    <item name="id_2" type="id"/>
    <!-- etc. -->
</resources>

Next create an integer array, in the same file or elsewhere (like in res/values/arrays.xml ) and use the ids as items:

<integer-array name="my_ids">
    <item>@id/id_1</item>
    <item>@id/id_2</item>
    <!-- etc. -->
</integer-array>

Now you can use the ids in your xml using android:id="@id/id_1" (without the + ), and also use the integer array in code:

int[] myIds = getResources().obtainTypedArray(R.array.my_ids);
for (int i = 0; ...) {
    int id = myIds.getResourceId(i, 0);
    // do something with id
}

I think there is no need to do like this. Your buttons ids are so similar. try this code:

for (int i = 0; i < count; i++) {
int idInt = res.getIdentifier(id, String.format("int%d", i), packageName);
Button button = (Button) findViewById(idInt);
button.setText("foo");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM