gpt4 book ai didi

java - 如何创建禁用其他按钮的功能

转载 作者:行者123 更新时间:2023-12-02 13:34:10 24 4
gpt4 key购买 nike

我正在创建一个问答游戏,可以通过选择答案按钮获取用户答案。我想创建一个功能,当用户点击按钮作为答案时禁用其他按钮(答案选择)。目前,当用户连续按下多个按钮时,答案(点击的答案)将用作下一个问题的答案,具体取决于点击的次数。我曾尝试放置一个 boolean 标志来禁用其他按钮,但它将无法转到下一个问题。

这是我的按钮代码。

public void onClick(View v) {
switch (v.getId()) {
case (R.id.choice1_Button) :
if (choice1.getText().equals(answer)) {
correct = 1;
sound(correct);
score += 10;
Toast.makeText(this, "Brilliant! The answer is correct", Toast.LENGTH_SHORT).show();
scoreboard(score);
getNextQuestion(counter);
}else {
correct = 0;
sound(correct);
Toast.makeText(this, "Nice try. The correct answer is "+answer, Toast.LENGTH_SHORT).show();
if (score > 0) {
score = score - 3;
}
if (score < 0){
score = 0;
}
scoreboard(score);
getNextQuestion(counter);
}
break;
case (R.id.choice2_Button) :
if (choice2.getText().equals(answer)) {
correct = 1;
sound(correct);
score += 10;
Toast.makeText(this, "Brilliant! The answer is correct", Toast.LENGTH_SHORT).show();
scoreboard(score);
getNextQuestion(counter);
}else {
correct = 0;
sound(correct);
Toast.makeText(this, "Nice try. The correct answer is "+answer, Toast.LENGTH_SHORT).show();
if (score > 0) {
score = score - 3;
}
if (score < 0){
score = 0;
}
scoreboard(score);
getNextQuestion(counter);
}
break;
case (R.id.choice3_Button) :
if (choice3.getText().equals(answer)) {
correct = 1;
sound(correct);
score += 10;
Toast.makeText(this, "Brilliant! The answer is correct", Toast.LENGTH_SHORT).show();
scoreboard(score);
getNextQuestion(counter);
}else {
correct = 0;
sound(correct);
Toast.makeText(this, "Nice try. The correct answer is "+answer, Toast.LENGTH_SHORT).show();
if (score > 0) {
score = score - 3;
}
if (score < 0){
score = 0;
}
scoreboard(score);
getNextQuestion(counter);
}
break;
}
}

这是我的 getNextQuestion 方法

private void getNextQuestion(final int i) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (counter < 6) {
setQuestion();
}else {
// if over 5 do this
Intent intent = new Intent(Level6.this, NextLevel.class);
Bundle a = new Bundle();
a.putInt("score", score);
a.putInt("level", 6);
intent.putExtras(a);
startActivity(intent);
finish();
}
}
},500);
}

这是我的 setQuestion() 方法

private void setQuestion() {
emotion_View.setImageResource(questionsLib.getQuestions(counter));
choice1.setText(questionsLib.getOptions1(counter));
choice2.setText(questionsLib.getOptions2(counter));
choice3.setText(questionsLib.getOptions3(counter));
answer = questionsLib.getCorrectAnswer(counter);
counter++;

}

最佳答案

制作禁用按钮组的方法,如下所示:

private void setQuestion(Boolean enable) {
choice1.setEnabled(enable);
choice2.setEnabled(enable);
choice3.setEnabled(enable);
}
  1. 然后在getNextQuestion之前执行此方法来设置按钮禁用(setQuestion(false))
  2. 设置下一个问题后,通过处理程序中的方法 (setQuestion(true)) 启用按钮。

或者您可以在 getNextQuestion 方法中全部设置:

private void getNextQuestion(final int i) {
setQuestion(false); //disable buttons
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (counter < 6) {
setQuestion();
}else {
// if over 5 do this
Intent intent = new Intent(Level6.this, NextLevel.class);
Bundle a = new Bundle();
a.putInt("score", score);
a.putInt("level", 6);
intent.putExtras(a);
startActivity(intent);
setQuestion(true); //enable buttons
finish();
}
}
},500);
}

关于java - 如何创建禁用其他按钮的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43097196/

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