gpt4 book ai didi

java - 在 Android 中以编程方式设置 ToggleButton 的状态?

转载 作者:行者123 更新时间:2023-11-29 22:04:21 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
ToggleButton state change programmatically rather than automatically in Android?

我正在尝试切换图像,这是一个带有切换按钮的数组。唯一的问题是我需要在三卷后重置切换按钮。我已经想出如何在 3 卷后重置图像;但是,我必须单击切换按钮两次才能使状态与切换按钮中的图像匹配。我在读this thread ,但是当我尝试将 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;
}

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com