1

可能重复:
ToggleButton 状态以编程方式更改而不是在 Android 中自动更改?

我正在尝试切换图像,一个带有切换按钮的数组。唯一的问题是我需要在三卷后重置切换按钮。我已经知道如何在 3 次滚动后重置图像;但是,我必须单击切换按钮两次才能使状态与切换按钮中的图像相匹配。我正在阅读这个线程,但是当我尝试将 setActivated(false) 应用于循环时,我得到了这个语法错误:

The method setActivated(boolean) is undefined for the type ToggleButton

按钮的 XML:

        <ToggleButton
            android:id="@+id/tbDice1"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:textOn="" 
            android:textOff=""              
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp" 
            android:background="@drawable/die_grn_6" />

        <ToggleButton
            android:id="@+id/tbDice2"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:textOn="" 
            android:textOff=""
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp" 
            android:background="@drawable/die_grn_6" />

        <ToggleButton
            android:id="@+id/tbDice3"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:textOn=""
            android:textOff=""
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp" 
            android:background="@drawable/die_grn_6" />

        <ToggleButton
            android:id="@+id/tbDice4"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:textOn=""
            android:textOff=""
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/die_grn_6" />

        <ToggleButton
            android:id="@+id/tbDice5"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:textOn="" 
            android:textOff=""
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/die_grn_6" />
        </TableRow>

听众的Java:

public void playGame()  
{  
    final Random rand = new Random();       

    dice0 = (ToggleButton)findViewById(R.id.tbDice1);  
    dice1 = (ToggleButton)findViewById(R.id.tbDice2);
    dice2 = (ToggleButton)findViewById(R.id.tbDice3);
    dice3 = (ToggleButton)findViewById(R.id.tbDice4);
    dice4 = (ToggleButton)findViewById(R.id.tbDice5);


    txtTurnNum = (TextView)findViewById(R.id.turnNum);
    txtRollNum = (TextView)findViewById(R.id.rollNum);



    final ToggleButton[] dice = {dice0, dice1, dice2, dice3, dice4}; //array of buttons (dice)
    final int [] diceValue = new int [5];
    final boolean [] isHeld = {false, false, false, false, false};  // array of dice to be held (hold)

    roll = (Button)findViewById(R.id.btnroll);
    score = (Button)findViewById(R.id.btnscore);        

    roll.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v ) {   
            rollDice(dice, diceValue, isHeld, rand);    
        }
    }); 

    score.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v ) {   
            scoreDice(diceValue);   
        }
    }); 

    dice0.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (dice0.isChecked()) {
                isHeld[0] = true;
                String imgName = "die_red_" + diceValue[0];                 
                int id = getResources().getIdentifier(imgName, "drawable", getPackageName());   
                dice0.setBackgroundResource(id);  //Changes to red
            } else {
                isHeld[0] = false;
                String imgName = "die_grn_" + diceValue[0];                 
                int id = getResources().getIdentifier(imgName, "drawable", getPackageName());   
                dice0.setBackgroundResource(id);  //Changes to green
            }
        }
    });

    dice1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (dice1.isChecked()) {
                isHeld[1] = true;
                String imgName = "die_red_" + diceValue[1];                 
                int id = getResources().getIdentifier(imgName, "drawable", getPackageName());   
                dice1.setBackgroundResource(id);  //Changes to red
            } else {
                isHeld[1] = false;
                String imgName = "die_grn_" + diceValue[1];                 
                int id = getResources().getIdentifier(imgName, "drawable", getPackageName());   
                dice1.setBackgroundResource(id);  //Changes to green
            }
        }
    });

    dice2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (dice2.isChecked()) {
                isHeld[2] = true;
                String imgName = "die_red_" + diceValue[2];                 
                int id = getResources().getIdentifier(imgName, "drawable", getPackageName());   
                dice2.setBackgroundResource(id);  //Changes to red
            } else {
                isHeld[2] = false;
                String imgName = "die_grn_" + diceValue[2];                 
                int id = getResources().getIdentifier(imgName, "drawable", getPackageName());   
                dice2.setBackgroundResource(id);  //Changes to green
            }
        }
    });

    dice3.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (dice3.isChecked()) {
                isHeld[3] = true;
                String imgName = "die_red_" + diceValue[3];                 
                int id = getResources().getIdentifier(imgName, "drawable", getPackageName());   
                dice3.setBackgroundResource(id);  //Changes to red
            } else {
                isHeld[3] = false;
                String imgName = "die_grn_" + diceValue[3];                 
                int id = getResources().getIdentifier(imgName, "drawable", getPackageName());   
                dice3.setBackgroundResource(id);  //Changes to green
            }
        }
    });

    dice4.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (dice4.isChecked()) {
                isHeld[4] = true;
                String imgName = "die_red_" + diceValue[4];                 
                int id = getResources().getIdentifier(imgName, "drawable", getPackageName());   
                dice4.setBackgroundResource(id);  //Changes to red
            } else {
                isHeld[4] = false;
                String imgName = "die_grn_" + diceValue[4];                 
                int id = getResources().getIdentifier(imgName, "drawable", getPackageName());   
                dice4.setBackgroundResource(id);  //Changes to green
            }
        }
    });
}

public int[] rollDice(ToggleButton [] dice, int [] diceValue, boolean [] isHeld, Random rand)
{


    if(rollNum < MAX_ROLLS){
        for (int i = 0; i < dice.length; i++) {
            if (!isHeld[i]) {
                int rndInt = rand.nextInt(6) + 1; // Random number between 1 and 6          
                String imgName = "die_grn_" + rndInt;                   
                int id = getResources().getIdentifier(imgName, "drawable", getPackageName());   
                diceValue[i] = rndInt;
                dice[i].setBackgroundResource(id);  //Loops through the dice array and sets the appropriate dice images based on individual randoms
            } else {
                //do nothing                        
            }
        }
        rollNum++;              
    } else {
        //Turn is over
        if(turnNum < MAX_TURNS){
            rollNum = 1;  //reset turn number to 1
            turnNum++;                  
            txtTurnNum.setText("" + turnNum);
            scoreDice(diceValue);
            updateScores(scoresArray);
            for (int i = 0; i <dice.length; i++) {
                if (isHeld[i]) {
                    isHeld[i] = false;
                    String imgName = "die_grn_" + diceValue[i];                 
                    int id = getResources().getIdentifier(imgName, "drawable", getPackageName());   
                    dice[i].setBackgroundResource(id);  //Changes to green
                    dice[i].setActivated(false);  //PROBLEM HERE
                }
            }
        } else {
            Toast.makeText(getApplicationContext(), "Game Over",
                    Toast.LENGTH_LONG).show();          
        }
    }

    txtRollNum.setText("" + rollNum);   



    return diceValue;
}

有什么想法可以为我指明正确的方向吗?

4

1 回答 1

3

我最终找到了这个线程,它给了我正确的 button.setChecked(true) 语法

于 2012-06-23T01:28:14.987 回答