[英]Is there any way to use incrementing number as call to methods with different number in every iteration
What I'm showing is what I'd like to get but I don't know if it is possible.我展示的是我想要得到的,但我不知道这是否可能。 Basically I want to find 48 ImageButton
views based on the index i
.基本上我想根据索引i
找到 48 个ImageButton
视图。
ImageButton[] buttons;
for (int i = 0; i < 48; i++) {
buttons[i] = (ImageButton) findViewById(R.id.k_i);
}
No that's not possible.不,那是不可能的。 Since you need to pass the view id which is an int
.因为您需要传递一个int
的视图 id。 Although you can use the method findViewWithTag which allows you to find views with a specified tag ( String
).尽管您可以使用findViewWithTag方法,该方法允许您查找具有指定标签( String
)的视图。 You only have to add the attribute android:tag="button_1"
in the views you want to find.您只需在要查找的视图中添加属性android:tag="button_1"
即可。 Then you can just do the following.然后,您可以执行以下操作。
String baseTag = "button_";
ImageButton[] buttons;
for (int i = 0; i < 48; i++) {
buttons[i] = (ImageButton) findViewWithTag(baseTag + i);
}
I think you can do it like this我认为你可以这样做
buttons[i] = v.findViewById(Context.getResources().getIdentifier("k_"+i,"id",Context.getPackageName()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.