添加值,更改单击单选按钮android

从来好事天生俭,自古瓜儿苦后甜。这篇文章主要讲述添加值,更改单击单选按钮android相关的知识,希望能为你提供帮助。
【添加值,更改单击单选按钮android】我的问题是,当我从radiobutton检查答案并想要更改其他radiobutton的检查答案时,点的总值增加(他计算答案的两个值)。我有7个RadioGroup,每个有3个RadioButton。是否可以阻止此计算?我希望有可能将我检查过的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 answerspublic 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>

答案我根据你的问题更新了我的答案,试试这个我也测试了它。
public class question1 extends AppCompatActivity {//Necessary to calculate points List< Integer> points; int score = 0; int numberofquestions = 7; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_question1); points = new ArrayList< Integer> (); for (int index = 0; index < numberofquestions; index++) { points.add(index, 0); }//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); } // Toast.makeText(getApplicationContext(), score + "", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(question1.this, fini.class); intent.putExtra("name", textView.getText().toString()); intent.putExtra("totalPoints", score); score=0; startActivity(intent); //score = 0; } }); } // Calculate totalPoints based on checked answerspublic List< Integer> onRadioButtonClicked(View view) { // Is the button now checked? boolean checked = ((RadioButton) view).isChecked(); switch (view.getId()) { case R.id.answerA1:points.set(0, 3); break; case R.id.answerB1:points.set(0, 2); break; case R.id.answerC1:points.set(0, 1); break; case R.id.answerA2:points.set(1, 3); break; case R.id.answerB2:points.set(1, 2); break; case R.id.answerC2:points.set(1, 1); break; case R.id.answerA3:points.set(2, 3); break; case R.id.answerB3:points.set(2, 2); break; case R.id.answerC3:points.set(2, 1); break; case R.id.answerA4:points.set(3, 3); break; case R.id.answerB4:points.set(3, 2); break; case R.id.answerC4:points.set(3, 1); break; case R.id.answerA5:points.set(4, 3); break; case R.id.answerB5:points.set(4, 2); break; case R.id.answerC5:points.set(4, 1); break; case R.id.answerA6:points.set(5, 3); break; case R.id.answerB6:points.set(5, 2); break; case R.id.answerC6:points.set(5, 1); break; case R.id.answerA7:points.set(6, 3); break; case R.id.answerB7:points.set(6, 2); break; case R.id.answerC7:points.set(6, 1); break; } return points; } }


    推荐阅读