java - 检查用户按下了哪个按钮并检查按钮文本

标签 java android methods

我是 Java 和编程方面的菜鸟,我正在制作一个应用程序,用户试图根据图片猜测城市。用户看到一张城市的图片,图片下方有三个按钮,其中有不同的答案。图片是从数组中随机生成的,并且按钮文本会发生变化,以便至少其中一个按钮具有正确的答案。我想要一个带有“正确”的 TextView 来显示用户是否正确,一个带有“不正确”的 TextView 来显示用户是否错误。按下任何按钮时都会显示文本,而按下具有正确文本的按钮时不会显示文本。所以这就是我尝试过并坚持下去的。是的,我知道我的代码中有很多错误,例如方法名称等。我稍后会更改这些。

我有三个 boolean 值设置为 false,它们代表按下了哪个按钮。稍后你会明白更多。

Boolean test1 = false;
Boolean test2 = false;
Boolean test3 = false;

主要我有三个按钮,它们都调用 checkanswer 函数。而且他们都在那里将自己的 boolean 值转换为 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 方法似乎是随机的。我在这里做错了什么?

最佳答案

我们能看到CorrectAnswer吗?还, 我立即注意到,您可以只传递某种排序,而不是使用 test1test2test3 来指示按下了哪个按钮将参数放入 checkAnswer 中,如 int button。 因此,第一个按钮的 onClick 看起来像这样,通过增加 1 来后续按钮:

public void onClick(View v) {
            checkanswer(1);
        }

checkanswer看起来像这样:

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();
}

所以试试这个。

关于java - 检查用户按下了哪个按钮并检查按钮文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59536699/

相关文章:

Java - 极坐标。给定一条线段 AB 和一个点 P,找到极线 AB 上最接近点 P 的点

Android findViewById() with onFling in ListView 问题

java - 如何识别 Java 字节码中的覆盖方法?

javascript - 子组件可以访问其包含组件的方法吗?

java - JDBC、SELECT 语句仅返回最后一条记录。

java - 解码加密消息

android - TextView 宽度超过 LinearLayout 宽度

java - Java API 和 Java 方法之间有什么区别

java - ConcurrentHashMap中 'thread-safe'的含义

java - 在 Windows 机器上监控 (java) 进程死亡的最佳方法是什么?