简体   繁体   English

Android Studio:使用radioButtons进行测验

[英]Android Studio: Using radioButtons for a quiz

So I'm trying to make an app where you select an answer from a radioButton within a radioGroup , and when you hit the Submit button it will change the textbox to say "Correct" or "Wrong answer", depending on which button was selected. 所以我正在尝试创建一个应用程序,您可以从radioGroupradioButton选择答案,当您点击“提交”按钮时,它会将textbox更改为“正确”或“错误答案”,具体取决于选择了哪个按钮。 I'm able to run the app, and select the radioButton , but when I click submit, the app crashes and says "Unfortunately, MyApp has stopped". 我可以运行应用程序,并选择radioButton ,但是当我点击提交时,应用程序崩溃并说“不幸的是,MyApp已停止”。

This is the code that I have: 这是我的代码:

XML XML

 <RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/question1"
    android:id="@+id/q1radiobox">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/q1a1"
            android:id="@+id/q1a1" />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/q1a2"
            android:id="@+id/q1a2"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/q1a3"
            android:id="@+id/q1a3"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/q1a4"
            android:id="@+id/q1a4"/>
    </RadioGroup>

    <Button
        android:onClick="checkResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/q1radiobox"
        android:text="Submit"/>

Java Java的

   private void checkResult() {
      RadioButton rb;
      rb = (RadioButton) findViewById(R.id.q1a3);

      if (rb.isChecked()) {
        ((TextView) findViewById(R.id.answer1)).setText("@string/correct");
      } 
      else {
          ((TextView) findViewById(R.id.answer1)).setText("@string/incorrect");
    }
} 

Any help would be greatly appreciated; 任何帮助将不胜感激; I can't work out what's wrong! 我无法弄清楚什么是错的!

EDIT: I have posted the solution. 编辑:我已经发布了解决方案。 Credit to @miselking for pointing out one of the issues. 感谢@miselking指出其中一个问题。

Ok, so what you need to know it that when you are using android:onClick="checkResult" way of defining the click events, your checkResult method needs to have the View as an argument, only then it can respond to onClick events. 好的,所以你需要知道的是,当你使用android:onClick="checkResult"定义点击事件的方式时,你的checkResult方法需要将View作为参数,然后它才能响应onClick事件。 So, change your method to something like this: 所以,将您的方法更改为以下内容:

    private void checkResult(View v) {
      RadioButton rb;
      rb = (RadioButton) findViewById(R.id.q1a3);

      if (rb.isChecked()) {
        ((TextView) findViewById(R.id.answer1)).setText("@string/correct");
      } 
      else {
          ((TextView) findViewById(R.id.answer1)).setText("@string/incorrect");
    }

EDIT: What you are getting is a warning, not an error. 编辑:你得到的是一个警告,而不是一个错误。 You are getting that warning because you are not using the parameter v anywhere in your method. 您收到该警告是因为您没有在方法中的任何位置使用参数v You can ignore it, it is usefull if you have multiple buttons invoking the same method, then you need to know which button actually invoked the method. 您可以忽略它,如果您有多个按钮调用相同的方法,那么您将需要知道哪个按钮实际调用了该方法。

Let's say you have two buttons with ids btnId1 and btnId2 . 假设你有两个带有id btnId1btnId2按钮。 Both of them have this line of code in xml file: android:onClick="checkResult" , so both of them call the same method (you can do that). 他们都在xml文件中有这行代码: android:onClick="checkResult" ,所以他们都调用相同的方法(你可以这样做)。 Then, when you click any of these two buttons, which one actually invoked the method? 然后,当您单击这两个按钮中的任何一个时,哪一个实际调用了该方法? Well, that is why the View v is neccessary. 好吧,这就是为什么View v是必要的。 Then, you would have been able to see which button was actually clicked and responded appropriatelly. 然后,您将能够看到实际单击了哪个按钮并且适当地响应。 Example of implementation of the checkResult : checkResult的实现示例:

public void checkResult(View view)
    {
        Log.d("TAG_BTN", "Someone called me. But who???");
        switch (view.getId())
        {
            case R.id.btnId1:
                Log.d("TAG_BTN", "BtnId1 called me. Do something, please...");
                break;
            case R.id.btnId2:
                Log.d("TAG_BTN", "BtnId2 called me. What next to do...");
                break;
        }
    }

Hopefully, you know understand why you need the View parameter. 希望您知道为什么需要View参数。

So after all of this hassle, I have found that the issues were as follows: 在经历了所有这些麻烦之后,我发现问题如下:

 private void checkResult()

Needed to be 需要

public void checkResult(View view)

Thank you to @miselking for pointing out the lack of a view, and I found the public/private void by pure luck, when playing around with the method. 感谢@miselking指出缺少一个视图,并且在玩这个方法的时候,我发现公共/私有虚空是纯粹的运气。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM