How can we use a variable in R.id

微笑、不失礼 提交于 2019-12-01 06:48:40

I solved it using getIdentifier method

Button[] buttons; 
for(int i=0; i<buttons.length; i++) {
{
   String buttonID = "sound" + (i+1);

   int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
   buttons[i] = ((Button) findViewById(resID));
   buttons[i].setOnClickListener(this);
}

the id is an intiger value not string: hope u ll get an idea from below code

 while (ctr<3)
{
   int layoutid;
    if(ctr==1)
    { layoutid = R.id.AbsoluteLayout1;}
    else{
     layoutid = R.id.AbsoluteLayout2;}
    mainlayout[ctr] = (AbsoluteLayout)findViewById(layoutid);
    ctr++;
}
---------------------------------------------

all these won post an error as they are handled as int itself manipulate as you want you cant use this as it is as

int[] ctra={R.id.xx,R.id.xxx};
  int i=0;
 while (ctr<3)
{
mainlayout[i]=(AbsoluteLayout)findViewById(ctra[i]);
i++;}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!