0

で 9 つのButton ビューを作成し、main.xmlそれに名前を付けてからButton1, Button2... Button9..、コードでボタンの を作成しました。ArrayListこのボタンの配列が私のボタンを保持main.xmlし、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
}

私の問題はR.id.Button1 .. R.id.Button3、findViewById メソッドに渡す必要があることですが、これをループする必要があります。とにかくfindViewByIdwhileループでカウンターを渡すことができますか?

お知らせ下さい。

4

2 に答える 2

1

あなたはこれを試すことができます。

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));
}
于 2012-05-26T07:09:17.660 に答える
1

このチェックのために尋ねられた同じ質問がすでにあると思います


質問する前にまず検索することをお勧めします

于 2012-05-26T07:10:20.723 に答える