反向数组和设置按钮

时间:2015-03-13 08:26:16

标签: java android

我想将值设置为数组的按钮。但是我想要按钮和数组的设定值反转。如果是数组setDay [100]和31数字按钮,则设定值设定日100-1和按钮31-1。

打印

Blockquote

 btn1  btn2  btn3  btn4  btn5 ....btn28  btn29  btn30  btn31

  69    70    71    72    73  ....  97    98      99    100

   int[] btnRes = {R.id.btn01, R.id.btn02, R.id.btn03, R.id.btn04, R.id.btn05, R.id.btn06, R.id.btn07, R.id.btn09,R.id.btn10, R.id.btn11, R.id.btn12, R.id.btn13, R.id.btn14, R.id.btn15, R.id.btn16, R.id.btn18,R.id.btn19, R.id.btn20, R.id.btn21, R.id.btn22, R.id.btn23, R.id.btn24, R.id.btn25, R.id.btn26,R.id.btn27,R.id.btn28,R.id.btn29,R.id.btn30};



/////// init buttons
      Button[] btnPrev = new Button[btnRes.length];

      for (int i = 0; i < btnPrev.length; ++i) {
          btnPrev[i] = (Button) findViewById(btnRes[i]);

      }

////// set value buttons to all buttons
      for (int i = 0; i < btnPrev.length; ++i) {

          btnPrev[i].setText(String.valueOf(setDay[i]));

      }

1 个答案:

答案 0 :(得分:0)

试试这个:

int maxDays = 100;
int buttonsNbr = btnPrev.length;

for (int i = buttonsNbr; i > 0; i--) {

    btnPrev[buttonsNbr - i].setText(String.valueOf(setDay[ maxDays - i]));

}