【问题标题】:Android Studio: Using radioButtons for a quizAndroid Studio:使用单选按钮进行测验
【发布时间】:2015-05-09 12:43:59
【问题描述】:

所以我正在尝试制作一个应用程序,您可以在其中从radioGroup 中的radioButton 中选择一个答案,当您点击提交按钮时,它会将textbox 更改为“正确”或“错误”答案”,具体取决于选择了哪个按钮。我可以运行该应用程序,并选择radioButton,但是当我单击提交时,应用程序崩溃并显示“不幸的是,MyApp 已停止”。

这是我的代码:

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

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

任何帮助将不胜感激;我不知道出了什么问题!

编辑:我已经发布了解决方案。感谢@miselking 指出其中一个问题。

【问题讨论】:

  • 在您的日志猫上发布错误消息。
  • 我还没看到。
  • 抱歉,所有待处理的编辑只是将“XML”更改为小写。
  • 日志告诉你,你正在调用一个名为 checkForResult 的函数,带有 view 参数,但你的函数似乎没有接受任何参数。

标签: java xml android-studio


【解决方案1】:

好的,所以你需要知道的是,当你使用android:onClick="checkResult"定义点击事件的方式时,你的checkResult方法需要有View作为参数,这样它才能响应onClick事件。所以,把你的方法改成这样:

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

编辑:您得到的是警告,而不是错误。您收到该警告是因为您没有在方法中的任何地方使用参数v。您可以忽略它,如果您有多个按钮调用相同的方法,这很有用,那么您需要知道哪个按钮实际调用了该方法。

假设您有两个按钮,ID 分别为 btnId1btnId2。它们在 xml 文件中都有这行代码:android:onClick="checkResult",所以它们都调用了相同的方法(你可以这样做)。那么,当您单击这两个按钮中的任何一个时,哪个实际调用了该方法?嗯,这就是为什么View v 是必要的。然后,您将能够看到实际单击了哪个按钮并做出了适当的响应。 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;
        }
    }

希望您知道为什么需要View 参数。

【讨论】:

  • 这个我已经试过了,没有任何影响。当我将鼠标悬停在代码的“查看 v”部分时,它会显示“从未使用过参数 'v'”。
  • 感谢您的编辑,是的,这已经清除了 View 的使用!解释得很好。
【解决方案2】:

所以经过这么多麻烦,我发现问题如下:

 private void checkResult()

需要

public void checkResult(View view)

感谢@miselking 指出缺乏视图,我在使用该方法时发现了纯运气的公共/私人空白。

【讨论】:

  • 没问题,很抱歉我花了一段时间才在编辑中写下解释,希望对您有所帮助...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2015-03-03
  • 2022-01-19
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多