내 문제는 내가 라디오 버튼에서 답을 확인하고 다른 라디오 버튼에 체크 대답을 변경하고자 할 때, 포인트 증가의 총 가치는 (그가 대답의 두 값을 모두 계산)이다. 나는 각각 7 개의 RadioGroup과 3 개의 RadioButton을 가지고있다. 이 계산을 차단할 수 있습니까? 총체적인 가치를 높이 지 않고 체크 라디오 버튼을 다른 라디오 버튼으로 바꿀 수있는 가능성을 갖고 싶습니다.추가 값, 클릭 라디오 버튼을 변경할 때 안드로이드
나는 당신의 문제에 따라 내 대답을 업데이트public class question1 extends AppCompatActivity {
//Necessary to calculate points
List<Integer> points;
int score = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
points = new ArrayList<Integer>();
//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) {
for (int index = 0; index < points.size(); index++) {
score = score + points.get(index);
Intent intent = new Intent(question1.this, fini.class);
intent.putExtra("name", textView.getText().toString());
intent.putExtra("totalPoints", score);
startActivity(intent);
}
}
});
}
// Calculate totalPoints based on checked answers
public List<Integer> 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;
points.set(0, 3);
// }
break;
case R.id.answerB1:
// if (checked) {
//points = points + 2;
points.set(0, 2);
// }
break;
case R.id.answerC1:
// if (checked) {
//points = points + 1;
points.set(0, 1);
// }
break;
case R.id.answerA2:
// if (checked) {
//points = points + 3;
points.set(1, 3);
// }
break;
case R.id.answerB2:
// if (checked) {
//points = points + 2;
points.set(1, 2);
// }
break;
case R.id.answerC2:
// if (checked) {
//points = points + 1;
points.set(1, 1);
//}
break;
case R.id.answerA3:
// if (checked) {
//points = points + 3;
points.set(2, 3);
//}
break;
case R.id.answerB3:
//if (checked) {
//points = points + 2;
points.set(2, 2);
// }
break;
case R.id.answerC3:
// if (checked) {
//points = points + 1;
points.set(2, 1);
//}
break;
case R.id.answerA4:
//if (checked) {
//points = points + 3;
points.set(3, 3);
//}
break;
case R.id.answerB4:
//if (checked) {
//points = points + 2;
points.set(3, 2);
//}
break;
case R.id.answerC4:
//if (checked) {
//points = points + 1;
points.set(3, 1);
//}
break;
case R.id.answerA5:
//if (checked) {
//points = points + 3;
points.set(4, 3);
//}
break;
case R.id.answerB5:
//if (checked) {
//points = points + 2;
points.set(5, 2);
//}
break;
case R.id.answerC5:
//if (checked) {
//points = points + 1;
points.set(4, 1);
//}
break;
case R.id.answerA6:
//if (checked) {
//points = points + 3;
points.set(5, 3);
//}
break;
case R.id.answerB6:
//if (checked) {
//points = points + 2;
points.set(5, 2);
//}
break;
case R.id.answerC6:
//if (checked) {
//points = points + 1;
points.set(5, 1);
//}
break;
case R.id.answerA7:
//if (checked) {
//points = points + 3;
points.set(6, 3);
//}
break;
case R.id.answerB7:
//if (checked) {
//points = points + 2;
points.set(6, 2);
//}
break;
case R.id.answerC7:
//if (checked) {
//points = points + 1;
points.set(6, 1);
//}
break;
}
return points;
}
}
<?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:onClick="onRadioButtonClicked"
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:onClick="onRadioButtonClicked"
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:onClick="onRadioButtonClicked"
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="#FFCDDC39" />
<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:onClick="onRadioButtonClicked"
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/a2"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerB2"
android:onClick="onRadioButtonClicked"
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/b2"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerC2"
android:onClick="onRadioButtonClicked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
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="#FFCDDC39" />
<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:onClick="onRadioButtonClicked"
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/a3"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerB3"
android:onClick="onRadioButtonClicked"
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/b3"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerC3"
android:onClick="onRadioButtonClicked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
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="#FFCDDC39" />
<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:onClick="onRadioButtonClicked"
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/a4"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerB4"
android:onClick="onRadioButtonClicked"
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/b4"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerC4"
android:onClick="onRadioButtonClicked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
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="#FFCDDC39" />
<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:onClick="onRadioButtonClicked"
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/a5"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerB5"
android:onClick="onRadioButtonClicked"
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/b5"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerC5"
android:onClick="onRadioButtonClicked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
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="#FFCDDC39" />
<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:onClick="onRadioButtonClicked"
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/a6"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerB6"
android:onClick="onRadioButtonClicked"
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/b6"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerC6"
android:onClick="onRadioButtonClicked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
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="#FFCDDC39" />
<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:onClick="onRadioButtonClicked"
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/a7"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerB7"
android:onClick="onRadioButtonClicked"
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/b7"
android:textSize="13sp"
tools:targetApi="jelly_bean" />
<RadioButton
android:id="@+id/answerC7"
android:onClick="onRadioButtonClicked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
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="#FFCDDC39" />
<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"
android:visibility="gone"/>
</LinearLayout>
</ScrollView>
라디오 버튼을 선택하지 않은 경우 값을 뺍니까? 그래서 당신은 라디오 버튼을 선택하지 – elmorabea
@elmorabea 내가 시도 '예, 아무것도 –