我有一个数组中的6个按钮和一个数组中的4个textViews。当单击一个随机按钮时,它的值将在textView [0]上设置,当点击下一个随机按钮时,它的值将在textView [1]上设置。我想要做的是当点击4个随机按钮时,应该禁用其余2个按钮,因为我只有4个textViews。我该怎么做?
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);
}
}
答案 0 :(得分:2)
如果我理解你,你就可以这样做:
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);
}
}
答案 1 :(得分:0)
要使按钮不可点击:
button[i].setClickable(false);
至于哪些按钮不可点击,我不确定你希望如何完成。
//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);
}
}
这是一种多种方式,但我希望它有所帮助。
答案 2 :(得分:0)
答案 3 :(得分:0)
我不确定你在寻找什么只是试试这个:
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);
}
}
}
}