Array of ImageButtons, assign R.view.id from a variable

前端 未结 3 1998
余生分开走
余生分开走 2021-02-14 23:05

Hey there. My app is going to be using an array of 64 ImageButtons (8x8), and they\'re all already declared in my XML Layout with names like one1, two5, eight8, etc. Rather than

3条回答
  •  日久生厌
    2021-02-14 23:52

    I like AndrewKS' for, it's more elegant. Just keep in mind that findViewById receives an integer rather than a String. So you will have to do something like:

    int resID = getResources().getIdentifier(btnID, "drawable", "com.your.package");
    musicGrid[i][j] = (ImageButton) findViewById(resID);
    

提交回复
热议问题