How can we use a variable in R.id

后端 未结 2 2016
孤街浪徒
孤街浪徒 2021-01-13 06:43

The content of xml are



        
相关标签:
2条回答
  • 2021-01-13 07:08

    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++;}
    
    0 讨论(0)
  • 2021-01-13 07:12

    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);
    }
    
    0 讨论(0)
提交回复
热议问题