Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 检索Main.xml中的按钮组_Android_Android Layout_Android Button - Fatal编程技术网

Android 检索Main.xml中的按钮组

Android 检索Main.xml中的按钮组,android,android-layout,android-button,Android,Android Layout,Android Button,我在main.xml中创建了九个buttonn视图,并将其命名为Button1,Button2。。。Button9..然后在我的代码中创建了Button的ArrayList,很明显,这个按钮数组将把我的按钮保存在main.xml中,根据我目前的知识,要在my main.xml中获取按钮,我需要使用findViewById方法,基本上我需要循环数组来获取按钮 ArrayList<Button> buttons; int MAXBTN = 9; for( int i = 0; i <

我在
main.xml
中创建了九个
button
n视图,并将其命名为
Button1,Button2。。。Button9..
然后在我的代码中创建了Button的
ArrayList
,很明显,这个按钮数组将把我的按钮保存在
main.xml
中,根据我目前的知识,要在my main.xml中获取按钮,我需要使用findViewById方法,基本上我需要循环数组来获取按钮

ArrayList<Button> buttons;
int MAXBTN = 9;
for( int i = 0; i < BTN; i++ )
{
    // Code here to use the findViewById method to get the button
}
ArrayList按钮;
int MAXBTN=9;
对于(int i=0;i
我的问题是我需要传递
R.id.Button1。。R.id.Button3
到findViewById方法,但我需要循环这个。在
findViewById
while循环中,我是否可以传递计数器

请告知。

您可以试试这个

ArrayList<Button> buttons;
int MAXBTN = 9;
for( int i = 0; i < BTN; i++ )
{
    String buttonID = "Button" + i+1 ;
    int resID = getResources().getIdentifier(buttonID, "id", "packageName");
    buttons.get(i) = ((Button) findViewById(resID));
}
ArrayList按钮;
int MAXBTN=9;
对于(int i=0;i
我想已经有同样的问题了



最好先搜索,然后再问

谢谢。你的包名(在java文件的顶部区域找到)。