私はクイズアプリを開発中です。 アプリには5つの質問があります。 質問ごとに4つのオプションがあります。 質問ごとに私はラジオ・グループを使用しています。 これまでは、正しい回答のラジオボタンがチェックされているときに、正しい回答ごとにトーストメッセージを表示することができました。 送信ボタンをクリックすると、トーストメッセージにクイズ結果が表示されます。 私は、私が表示したい私のJavaコード内のポイントを言及しています。それらはsubmitQuizメソッドにあります。 以下、私は自分のアプリケーションのコードを提供しています。クイズアプリのラジオボタンのトラブル
XMLコード:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.infinitystone.mani.quiz.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:gravity="center_horizontal"
android:text="General Knowledge Quiz"
android:textAllCaps="true"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="italic" />
<EditText
android:id="@+id/name_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="8dp"
android:hint="Enter Your Name"
android:inputType="textCapWords" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="8dp"
android:text="Question 1:"
android:textAllCaps="true"
android:textColor="@android:color/black" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="8dp"
android:text="@string/question_1"
android:textColor="@android:color/background_dark" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="8dp"
android:orientation="vertical">
<RadioButton
android:id="@+id/radio_answer_1a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_1a" />
<RadioButton
android:id="@+id/radio_answer_1b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_1b" />
<RadioButton
android:id="@+id/radio_answer_1c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_1c" />
<RadioButton
android:id="@+id/radio_answer_1d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_1d" />
</RadioGroup>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="152dp"
android:layout_marginTop="8dp"
android:background="@android:color/black">
</View>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="8dp"
android:text="Question 2:"
android:textAllCaps="true"
android:textColor="@android:color/black" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="8dp"
android:text="@string/question_2"
android:textColor="@android:color/background_dark" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="8dp"
android:orientation="vertical">
<RadioButton
android:id="@+id/radio_answer_2a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_2a" />
<RadioButton
android:id="@+id/radio_answer_2b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_2b" />
<RadioButton
android:id="@+id/radio_answer_2c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_2c" />
<RadioButton
android:id="@+id/radio_answer_2d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_2d" />
</RadioGroup>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="152dp"
android:layout_marginTop="8dp"
android:background="@android:color/black">
</View>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="8dp"
android:text="Question 3:"
android:textAllCaps="true"
android:textColor="@android:color/black" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="8dp"
android:text="@string/question_3"
android:textColor="@android:color/background_dark" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="8dp"
android:orientation="vertical">
<RadioButton
android:id="@+id/radio_answer_3a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_3a" />
<RadioButton
android:id="@+id/radio_answer_3b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_3b" />
<RadioButton
android:id="@+id/radio_answer_3c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_3c" />
<RadioButton
android:id="@+id/radio_answer_3d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_3d" />
</RadioGroup>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="152dp"
android:layout_marginTop="8dp"
android:background="@android:color/black">
</View>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="8dp"
android:text="Question 4:"
android:textAllCaps="true"
android:textColor="@android:color/black" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="8dp"
android:text="@string/question_4"
android:textColor="@android:color/background_dark" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="8dp"
android:orientation="vertical">
<RadioButton
android:id="@+id/radio_answer_4a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_4a" />
<RadioButton
android:id="@+id/radio_answer_4b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_4b" />
<RadioButton
android:id="@+id/radio_answer_4c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_4c" />
<RadioButton
android:id="@+id/radio_answer_4d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_4d" />
</RadioGroup>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="152dp"
android:layout_marginTop="8dp"
android:background="@android:color/black">
</View>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="8dp"
android:text="Question 5:"
android:textAllCaps="true"
android:textColor="@android:color/black" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="8dp"
android:text="@string/question_5"
android:textColor="@android:color/background_dark" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="8dp"
android:orientation="vertical">
<RadioButton
android:id="@+id/radio_answer_5a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_5a" />
<RadioButton
android:id="@+id/radio_answer_5b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_5b" />
<RadioButton
android:id="@+id/radio_answer_5c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_5c" />
<RadioButton
android:id="@+id/radio_answer_5d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioGroup"
android:paddingLeft="8dp"
android:text="@string/answer_5d" />
</RadioGroup>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="152dp"
android:layout_marginTop="8dp"
android:background="@android:color/black">
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:onClick="submitQuiz"
android:text="Submit"
android:textAllCaps="true" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="8dp"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:onClick="resetQuiz"
android:text="Reset"
android:textAllCaps="true" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Javaコード:
package com.infinitystone.mani.quiz;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// This method is called when the radio button with correct answer is checked
public void radioGroup(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radio_answer_1b:
if (checked) {
Toast.makeText(this, "Correct answer",
Toast.LENGTH_SHORT).show();
}
break;
case R.id.radio_answer_2a:
if (checked) {
Toast.makeText(this, "Correct answer",
Toast.LENGTH_SHORT).show();
}
break;
case R.id.radio_answer_3b:
if (checked) {
Toast.makeText(this, "Correct answer",
Toast.LENGTH_SHORT).show();
}
break;
case R.id.radio_answer_4a:
if (checked) {
Toast.makeText(this, "Correct answer",
Toast.LENGTH_SHORT).show();
}
break;
case R.id.radio_answer_5d:
if (checked) {
Toast.makeText(this, "Correct answer",
Toast.LENGTH_SHORT).show();
}
break;
}
}
// This method is called when the submit button is clicked
public void submitQuiz(View view){
/*
* I want to display the following details when the submit button is clicked
*
* Number of questions attempted
* Number of right answers
* Number of wrong answers
*
* All this summary in a toast message
* */
}
}
string.xmlをファイル:
<resources>
<string name="app_name">Quiz</string>
<string name="question_1">Hitler party which came into power in 1933 is known as ?</string>
<string name="answer_1a">Labour Party</string>
<string name="answer_1b">Nazi Party</string>
<string name="answer_1c">Ku-Klux-Klan</string>
<string name="answer_1d">Democratic Party</string>
<!-- Correct answer: Nazi party -->
<string name="question_2">First human heart transplant operation conducted by Dr. Christiaan Barnard on Louis Washkansky, was conducted in which year ?</string>
<string name="answer_2a">1967</string>
<string name="answer_2b">1968</string>
<string name="answer_2c">1958</string>
<string name="answer_2d">1922</string>
<!-- Correct answer: 1967 -->
<string name="question_3">In which year Georgia, Uzbekistan and Turkmenistan became the members of UN ?</string>
<string name="answer_3a">1991</string>
<string name="answer_3b">1992</string>
<string name="answer_3c">1993</string>
<string name="answer_3d">1994</string>
<!-- Correct answer: 1992 -->
<string name="question_4">Escape velocity of a rocket fired from the earth towards the moon is a velocity to get rid of what ?</string>
<string name="answer_4a">Earth gravitational pull</string>
<string name="answer_4b">Moon gravitational pull</string>
<string name="answer_4c">Centripetal force due to the earth rotation</string>
<string name="answer_4d">Pressure of the atmosphere</string>
<!-- Correct answer: Earth gravitational pull -->
<string name="question_5">Which instrument is used for seeing objects at the surface of water from a submarine under water ?</string>
<string name="answer_5a">Kaleidoscope</string>
<string name="answer_5b">Spectroscope</string>
<string name="answer_5c">Telescope</string>
<string name="answer_5d">Periscope</string>
<!-- Correct answer: Periscope -->
</resources>
送信ボタンと各ラジオグループのIDも割り当てる必要がありますか? – mani
はい。それ以外の場合、どのボタンがクリックされたのか分かりません –
私はあなたのコードを正しく理解していませんでした。 もっと詳しいコードを教えてください。 – mani