简体   繁体   English

有没有办法在每次迭代中使用递增数字作为对具有不同数字的方法的调用

[英]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.

相关问题 Java-我可以在远程对象中调用任意数量的方法吗? - Java - Can I call any number of methods in my remote object? 有条件地调用多个方法直到满足要求的最佳方式 - Best way to conditionally call a number of methods until a requirement is met 在2种方法中使用随机生成的数字,然后再次调用随机生成器 - use a random generated number in 2 methods and call random generator again 如何在Java中调用具有任意数量的参数和任意数据类型的任何方法 - How to call any methods having any number of parameter and any datatype in Java 是否有更好的迭代方法来找到均匀可分的数字? - Is there better way of iteration to find the evenly divisible number? 递增 for 循环的每隔一次迭代(JAVA) - Incrementing every other iteration of for loop (JAVA) 按指定顺序递增数字 - Incrementing the number in a specified sequence 递归-递增数字并返回 - Recursion - incrementing a number and returning 正确的方法是使用BufferdOutputStream来编写大量不同的小文件 - The right way to use BufferdOutputStream to write a large number of different small files Java中对象到数字到数字的转换。 通用库中有任何方法吗? - Object to number to number conversion in Java. Any methods in common libraries?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM