我有7个问题(7个RadioGroup),每个问题有3个答案(总共21个答案)。我的问题是我的观点错误地提交了。当我检查所有答案A时,提交的分数是18,而不是21。同样,当我检查所有答案B时,分数是12,答案C - 分数6.我不明白为什么,以及如何解决它
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="25dp"
android:orientation="vertical"
tools:context="com.example.android.quizapp.question1">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="15dp"
android:layout_marginTop="8dp"
android:padding="10dp"
android:fontFamily="serif-monospace"
android:text="@string/question1"
android:textAlignment="center"
android:textSize="20sp"
tools:targetApi="jelly_bean" />
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/radioGroup1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<RadioButton
android:id="@+id/answerA1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:fontFamily="monospace"
android:text="@string/a1"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerB1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:fontFamily="monospace"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/b1"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerC1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/c1"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
</RadioGroup>
<TextView
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorPrimaryDark" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="15dp"
android:layout_marginTop="8dp"
android:padding="10dp"
android:fontFamily="serif-monospace"
android:text="@string/question2"
android:textAlignment="center"
android:textSize="20sp"
tools:targetApi="jelly_bean" />
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/radioGroup2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<RadioButton
android:id="@+id/answerA2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:fontFamily="monospace"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:onClick="onRadioButtonClicked"
android:text="@string/a2"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerB2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:fontFamily="monospace"
android:onClick="onRadioButtonClicked"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/b2"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerC2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:onClick="onRadioButtonClicked"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/c2"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:background="@color/colorPrimaryDark" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="15dp"
android:layout_marginTop="8dp"
android:padding="10dp"
android:fontFamily="serif-monospace"
android:text="@string/question3"
android:textAlignment="center"
android:textSize="20sp"
tools:targetApi="jelly_bean" />
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/radioGroup3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<RadioButton
android:id="@+id/answerA3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:fontFamily="monospace"
android:onClick="onRadioButtonClicked"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/a3"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerB3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:fontFamily="monospace"
android:onClick="onRadioButtonClicked"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/b3"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerC3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:onClick="onRadioButtonClicked"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/c3"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:background="@color/colorPrimaryDark" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="15dp"
android:layout_marginTop="8dp"
android:fontFamily="serif-monospace"
android:text="@string/question4"
android:textAlignment="center"
android:padding="10dp"
android:textSize="20sp"
tools:targetApi="jelly_bean" />
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/radioGroup4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<RadioButton
android:id="@+id/answerA4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:fontFamily="monospace"
android:onClick="onRadioButtonClicked"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/a4"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerB4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:fontFamily="monospace"
android:onClick="onRadioButtonClicked"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/b4"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerC4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:onClick="onRadioButtonClicked"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/c4"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:background="@color/colorPrimaryDark" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="15dp"
android:layout_marginTop="8dp"
android:fontFamily="serif-monospace"
android:text="@string/question5"
android:textAlignment="center"
android:padding="10dp"
android:textSize="20sp"
tools:targetApi="jelly_bean" />
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/radioGroup5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<RadioButton
android:id="@+id/answerA5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:fontFamily="monospace"
android:onClick="onRadioButtonClicked"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/a5"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerB5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:fontFamily="monospace"
android:onClick="onRadioButtonClicked"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/b5"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerC5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:onClick="onRadioButtonClicked"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/c5"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:background="@color/colorPrimaryDark" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="15dp"
android:layout_marginTop="8dp"
android:fontFamily="serif-monospace"
android:text="@string/question6"
android:textAlignment="center"
android:padding="10dp"
android:textSize="20sp"
tools:targetApi="jelly_bean" />
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/radioGroup6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<RadioButton
android:id="@+id/answerA6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:fontFamily="monospace"
android:onClick="onRadioButtonClicked"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/a6"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerB6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:fontFamily="monospace"
android:onClick="onRadioButtonClicked"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/b6"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerC6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:onClick="onRadioButtonClicked"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/c6"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:background="@color/colorPrimaryDark" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="15dp"
android:layout_marginTop="8dp"
android:padding="10dp"
android:fontFamily="serif-monospace"
android:text="@string/question7"
android:textSize="20sp"
tools:targetApi="jelly_bean" />
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/radioGroup7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<RadioButton
android:id="@+id/answerA7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:fontFamily="monospace"
android:onClick="onRadioButtonClicked"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/a7"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerB7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:fontFamily="monospace"
android:onClick="onRadioButtonClicked"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/b7"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerC7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:onClick="onRadioButtonClicked"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/c7"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
</RadioGroup>
<TextView
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:background="@color/colorPrimaryDark" />
<Button
android:id="@+id/so"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:fontFamily="serif-monospace"
android:text="@string/Finish"
android:textSize="20sp"
tools:targetApi="jelly_bean" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pName"
android:text="0"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="showScore" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scores"
android:text="0"/>
</LinearLayout>
</ScrollView>
我的java:
public class question1 extends AppCompatActivity {
//Necessary to calculate points
int points = 0;
TextView scoreTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
//Needed to sending total scores to fini.class
final TextView scoreTextView = ( TextView ) findViewById(R.id.scores);
//Passing editText value to this activity
final TextView textView = ( TextView ) findViewById(R.id.pName);
Intent intent = getIntent();
String str = intent.getStringExtra("name");
textView.setText(str);
//Sending editText value to next activity
final Button start = ( Button ) findViewById(R.id.so);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(question1.this, fini.class);
intent.putExtra("name", textView.getText().toString());
intent.putExtra("scores", scoreTextView.getText().toString());
startActivity(intent);
}
});
}
/**
* This method is called when we want to calculate points from answers
*/
public int onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = (( RadioButton ) view).isChecked();
switch (view.getId()) {
case R.id.answerA1:
if (checked) {
points = points + 3;
}break;
case R.id.answerB1:
if (checked) {
points = points + 2;
}break;
case R.id.answerC1:
if (checked) {
points = points + 1;
}break;
case R.id.answerA2:
if (checked) {
points = points + 3;
}break;
case R.id.answerB2:
if (checked) {
points = points + 2;
}break;
case R.id.answerC2:
if (checked) {
points = points + 1;
}break;
case R.id.answerA3:
if (checked) {
points = points + 3;
}break;
case R.id.answerB3:
if (checked) {
points = points + 2;
}break;
case R.id.answerC3:
if (checked) {
points = points + 1;
}break;
case R.id.answerA4:
if (checked) {
points = points + 3;
}break;
case R.id.answerB4:
if (checked) {
points = points + 2;
}break;
case R.id.answerC4:
if (checked) {
points = points + 1;
}break;
case R.id.answerA5:
if (checked) {
points = points + 3;
}break;
case R.id.answerB5:
if (checked) {
points = points + 2;
}break;
case R.id.answerC5:
if (checked) {
points = points + 1;
}break;
case R.id.answerA6:
if (checked) {
points = points + 3;
}break;
case R.id.answerB6:
if (checked) {
points = points + 2;
}break;
case R.id.answerC6:
if (checked) {
points = points + 1;
}break;
case R.id.answerA7:
if (checked) {
points = points + 3;
}break;
case R.id.answerB7:
if (checked) {
points = points + 2;
}break;
case R.id.answerC7:
if (checked) {
points = points + 1;
}break;
}
return points;
}
public void showScore(View view) {
scoreTextView.setText(" " + points);
}
}
调试kod,当我只选中A答案时
W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
D/OpenGLRenderer: endAllActiveAnimators on 0x79b66e4800 (RippleDrawable) with handle 0x79b3980040
D/ViewRootImpl@27b6561[MainActivity]: mHardwareRenderer.destroy()#1
D/ViewRootImpl@27b6561[MainActivity]: Relayout returned: oldFrame=[0,0][1440,2560] newFrame=[0,0][1440,2560] result=0x5 surface={isValid=false 0} surfaceGenerationChanged=true
D/ViewRootImpl@27b6561[MainActivity]: mHardwareRenderer.destroy()#4
D/ViewRootImpl@27b6561[MainActivity]: dispatchDetachedFromWindow
D/InputTransport: Input channel destroyed: fd=71
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 1
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 1
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 1
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 1
I/art: Do full code cache collection, code=252KB, data=237KB
I/art: After code cache collection, code=195KB, data=171KB
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 1
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 1
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 1
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 1
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 1
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 1
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 1
I/art: Do partial code cache collection, code=251KB, data=223KB
I/art: After code cache collection, code=251KB, data=223KB
I/art: Increasing code cache capacity to 1024KB
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 1
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 1
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 1
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@fad30a5[question1]: ViewPostImeInputStage processPointer 1
V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@f1f53a3 nm : com.example.android.quizapp ic=null
I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus
D/InputTransport: Input channel constructed: fd=77
D/InputTransport: Input channel destroyed: fd=91
D/ViewRootImpl@fad30a5[question1]: MSG_WINDOW_FOCUS_CHANGED 0
答案 0 :(得分:0)
您尚未使用ids onRadioButtonClicked
,answerA1
和answerB1
answerC1
分配给第一个RadioGroup
在您的布局中,检查第一个RadioButton
的{{1}}。前三个答案在其布局中缺少行RadioGroup
。比较
android:onClick="onRadioButtonClicked"
带
<RadioButton
android:id="@+id/answerA1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:fontFamily="monospace"
android:text="@string/a1"
android:textSize="13sp"
tools:targetApi="jelly_bean" />