Java 检查用户按下的按钮和按钮文本
我是一个Java和编程的noob,我正在制作一个应用程序,用户试图根据图片猜一个城市。用户看到一张城市的图片,图片下方有三个按钮,其中有不同的答案。图片从一个数组中随机排列,按钮文本会发生变化,以便至少有一个按钮有正确的答案。我想要一个正确的文本视图来显示用户是否正确,一个错误的文本视图来显示用户是否错误。文本在按下任何按钮时显示,而在按下具有正确文本的按钮时不显示。所以这就是我一直在尝试和坚持的。是的,我知道我的代码中有很多错误,比如方法的名称等等。我以后会换的 我有三个布尔值被设置为false,它们代表按下哪个按钮。你以后会明白的Java 检查用户按下的按钮和按钮文本,java,android,methods,Java,Android,Methods,我是一个Java和编程的noob,我正在制作一个应用程序,用户试图根据图片猜一个城市。用户看到一张城市的图片,图片下方有三个按钮,其中有不同的答案。图片从一个数组中随机排列,按钮文本会发生变化,以便至少有一个按钮有正确的答案。我想要一个正确的文本视图来显示用户是否正确,一个错误的文本视图来显示用户是否错误。文本在按下任何按钮时显示,而在按下具有正确文本的按钮时不显示。所以这就是我一直在尝试和坚持的。是的,我知道我的代码中有很多错误,比如方法的名称等等。我以后会换的 我有三个布尔值被设置为fals
Boolean test1 = false;
Boolean test2 = false;
Boolean test3 = false;
总的来说,我有三个按钮,它们都调用checkanswer功能。此外,它们都将自己的布尔值转换为true,这一点你很快就会知道。其中一个按钮的示例
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
test1 = true;
checkanswer();
}
});
这里是checkanswer函数
public void checkanswer() {
DisplayRandomImage();
//Giving three strings random city names from the "cities" array.
Random rndBtnTxt = new Random();
String randomCity1 = cities[rndBtnTxt.nextInt(cities.length)];
String randomCity2 = cities[rndBtnTxt.nextInt(cities.length)];
String randomCity3 = cities[rndBtnTxt.nextInt(cities.length)];
//Setting the random city names to the three different buttons.
btn1.setText(randomCity1);
btn2.setText(randomCity2);
btn3.setText(randomCity3);
//takes the picked image from the "DisplayRandomImage" method.
String str = String.valueOf(pickedImg);
//Tells what to call the different pictures, they are known as numbers make sure they are given names instead.
if (pickedImg == 0)
str = "venice";
if (pickedImg == 1)
str = "new york";
//If-statement checking so that atleast one button has the correct answer.
if (randomCity1 != str || randomCity2 != str || randomCity3 != str) {
Random rndbtn = new Random();
Button x = btnArray.get(rndbtn.nextInt(btnArray.size()));
//Sets one of the three buttons so that it has the correct answer.
x.setText(str);
}
//See where the correct answer is
String buttonText1 = btn1.getText().toString();
String buttonText2 = btn2.getText().toString();
String buttonText3 = btn3.getText().toString();
//check if the button that the user pressed has the correct answer
if (test1.equals(true) && buttonText1.equals(str)){
CorrectAnswer();
test1 = false;
}
if (test2.equals(true) && buttonText2.equals(str)){
CorrectAnswer();
test2 = false;
}
if (test3.equals(true) && buttonText3.equals(str)){
CorrectAnswer();
test3 = false;
}
else
WrongAnswer();
}
我不确定我在这里做错了什么。例如,当我按下btn1时,test1被设置为True,如果buttontext1与str的值相同,那么它应该可以工作。但出于某种原因,这三个按钮中的哪一个调用了CorrectAnswer方法似乎是随机的。我做错了什么?我们能看到正确的答案吗?而且
一开始,我就注意到,不用test1、test2和test3来指示按下了哪个按钮,只需将某种参数传递给checkAnswer,比如int按钮。
所以onClick对于第一个按钮和后续按钮是这样的,通过增加1:
public void onClick(View v) {
checkanswer(1);
}
检查答案如下所示:
public void checkanswer(int button) {
... (previous stuff) ...
//check if the button that the user pressed has the correct answer
if (button == 1 && buttonText1.equals(str)){
CorrectAnswer();
}
if (button == 2 && buttonText2.equals(str)){
CorrectAnswer();
}
if (button == 3 && buttonText3.equals(str)){
CorrectAnswer();
}
else
WrongAnswer();
}
所以请尝试一下。如果您只向我们展示完整代码的片段,那么很难判断错误在哪里。 错误可能是,例如,在正确答案中 我建议将onlick侦听器绑定到按钮,而不是更改布尔值。 请在此查看: 此外,我注意到另一个错误:
randomCity1 != str || randomCity2 != str || randomCity3 != str
如果至少有一个字符串不包含正确答案,则返回true
您可能希望输入if语句,但此时还没有包含正确答案的按钮。这是您希望使用的:
randomCity1 != str && randomCity2 != str && randomCity3 != str
编辑:查看条形码的答案,了解使用onClicklisteners的另一个示例。谢谢你们回答这个问题,我找到了一种方法来完成这个问题:
public void testingMethod(int button){
switch(button){
case 1:
if (buttonText1 == str)
CorrectAnswer();
else
WrongAnswer();
break;
case 2:
if (buttonText2 == str)
CorrectAnswer();
else
WrongAnswer();
break;
case 3:
if (buttonText3 == str)
CorrectAnswer();
else
WrongAnswer();
break;
}
}
既然你想知道这个方法的正确答案是什么样子的,是的,我知道可能没有必要使用这个方法,但我毕竟是noob
public void CorrectAnswer() {
findViewById(R.id.txtIncorrect).setVisibility(View.GONE);
findViewById(R.id.txtCorrect).setVisibility(View.VISIBLE);
}