[英]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. 我希望能够以XML格式分配视图ID,但是可以通过编程方式将它们作为整数访问,以便轻松地迭代它们。
Here's the direction I was going which does not work: 这是我前进的方向不起作用:
values/value.xml: 值/ value.xml:
<resources>
<integer name="int1">101</integer>
<integer name="int2">102</integer>
...
<integer name="int10">110</integer>
</resources>
layout/fancy_dialog_fragment.xml: 布局/ 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: 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): 这将进入res / values / arrays.xml(您可以随意调用它):
<?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: 创建文件
res/values/ids.xml
并在那里声明id:
<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: 接下来在同一个文件或其他地方(比如
res/values/arrays.xml
)创建一个整数数组,并使用id作为项:
<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: 现在你可以使用
android:id="@id/id_1"
(不带+
)在xml中使用android:id="@id/id_1"
,并在代码中使用整数数组:
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.
你的按钮ID非常相似。 try this code:
试试这段代码:
for (int i = 0; i < count; i++) {
for(int i = 0; i <count; i ++){
int idInt = res.getIdentifier(id, String.format("int%d", i), packageName);int idInt = res.getIdentifier(id,String.format(“int%d”,i),packageName);
Button button = (Button) findViewById(idInt);按钮=(按钮)findViewById(idInt);
button.setText("foo");button.setText( “富”);
}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.