1

i have 6 buttons in an array and 4 textViews in an array. when a random button is clicked its value will be set on textView[0] and when the next random button is clicked its value will be set on textView[1]. what i'm trying to do is when 4 random buttons are clicked the 2 remaining buttons should be disabled since i only have 4 textViews. how should i do this?

    textView[0] = (TextView)findViewById(R.id.t1);
    textView[1] = (TextView)findViewById(R.id.t2);
    textView[2] = (TextView)findViewById(R.id.t3);
    textView[3] = (TextView)findViewById(R.id.t4);
    final Button button[] = new Button[6];
    button[0] = (Button)findViewById(R.id.btn1);
    button[1] = (Button)findViewById(R.id.btn2);
    button[2] = (Button)findViewById(R.id.btn3);
    button[3] = (Button)findViewById(R.id.btn4);
    button[4] = (Button)findViewById(R.id.btn5);
    button[5] = (Button)findViewById(R.id.btn6);


    for(int i = 0; i <4; i++){
        if (button[i].getId() == v.getId()){
            textView[x].setText(button[i].getText()); //x=0
            button[i].setVisibility(View.INVISIBLE);
                if(x<6)
                    x++;
            }
    }

    for(int i = 0; i <6; i++){
        if(button[i].getVisibility() == View.VISIBLE){
            button[i].setEnabled(false);
        }
    }
mickey
  • 53
  • 1
  • 6

3 Answers3

2

If I understand you, you can do it just like this:

 for(int i = 0; i <6; i++){
    if (button[i].getId() == v.getId()){
        textView[x].setText(button[i].getText()); //x=0
        button[i].setVisibility(View.INVISIBLE);
            if(x<4)
                x++;
         break;
        }
}
if(x==4)
for(int i = 0; i <6; i++){
    if(button[i].getVisibility() == View.VISIBLE){
        button[i].setEnabled(false);
    }
}
Saif Hamed
  • 1,084
  • 1
  • 12
  • 17
0

To make a button not clickable:

     button[i].setClickable(false);

As far as which buttons to make not clickable, I am not sure how you want this done.

  //assuming this is all inside a onClickListener
 //first create a counter. 
 int ButtonCounter = 0;
 // Keep track of what buttons are clicked
 Boolean button0 = false, button1 = false, button2 = false, button3 = false, 
           button4 = fasle, button4 = false, button5 = false;


 //inside the onClick
 ButtonCounter++;
 switch (id){

     case R.id.btn1:
        button0 = true;
        button[0].setClickable(false);
        break;
     case R.id.btn2:
       button1 = true;
        button[1].setClickable(false);
       break;
     case R.id.btn3:
        button2 = true;
        button[2].setClickable(false);
        break;
     case R.id.btn4:
       button3 = true;
        button[3].setClickable(false);
       break;
     case R.id.btn5:
        button4 = true;
        button[4].setClickable(false);
        break;
     case R.id.btn6:
       button5 = true;
        button[5].setClickable(false);
       break;
  }
  if(ButtonCounter >= 4){
     for(int i = 0; i < button.size; i ++){
         button[i].setClickable(false);
        }
     }

This is kind of a scrappy way to do it, but I hope it helps.

Chad Bingham
  • 32,650
  • 19
  • 86
  • 115
  • i have 6 buttons. if four random buttons are clicked the remaining buttons should be disabled. for example,if i clicked button1, button5, button3 and button6, button2 and button4 should be disabled – mickey Jan 02 '14 at 17:58
  • Got it. Ok give me a second. – Chad Bingham Jan 02 '14 at 17:59
0

i am not sure what you are looking for just try this:

for(int i = 0; i <4; i++){

        if (button[i].getId() == v.getId()){
            textView[x].setText(button[i].getText()); //x=0
            button[i].setVisibility(View.INVISIBLE);
            Counter+=1;
            OpenbuttonID.add(button[i].getId());
            TryDisableAllbutton();
                if(x<6)
                    x++;
            }
    }

    ArrayList<Integer> OpenbuttonID = new ArrayList<Integer>();
    int Counter = 0;
    public void TryDisableAllbutton() {
        if (Counter >= 4) {
            for (int i = 0; i < 6; i++) {
                if (!OpenbuttonID.contains(button[i].getId())) {
                    button[i].setEnabled(false);
                }
            }
        }
    }
Sunny
  • 219
  • 1
  • 10