Android 中的 RadioButton 和 RadioGroup 使用切换 feature/isChecked 并清理代码
Issue with RadioButton with RadioGroup in Android using a toggle feature/isChecked and cleaning code up
您好,我已经尝试了一段时间并研究了很多理论和堆栈答案。虽然我只是不知道如何将下面的代码用于一个简单的琐事应用程序,使用 RadioGroup 中的 RadioButton,然后在选中时添加一个来评分。问题是,如果您一直按正确答案,分数只会增加。如果答案改变点相应调整,我想应用某种类型的切换。
我也想清理这段代码,以免重复。使用新的 class 或方法。虽然我不知道如何实现它。
主要
package com.example.samuel.trivaapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//Score keeper.
private int score = 0;
private int correct = 0;
private int wrong = 0;
// determines if the selected was right or wrong, then used to add up the score.
boolean question1 = false;
boolean question2 = false;
boolean question3 = false;
// private boolean question3 = false;
// private boolean question4 = false;
// private boolean question5 = false;
// private boolean question6 = false;
// private boolean question7 = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup rGroup1 = (RadioGroup) findViewById(R.id.first_question_group);
RadioButton checkedRadioButton1 = (RadioButton) rGroup1.findViewById(rGroup1.getCheckedRadioButtonId());
rGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (R.id.correct_answer1 == checkedId) {
question1 = true;
} else {
question1 = false;
}
}
});
RadioGroup rGroup2 = (RadioGroup) findViewById(R.id.second_question_group);
RadioButton checkedRadioButton2
= (RadioButton) rGroup2.findViewById(rGroup2.getCheckedRadioButtonId());
rGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (R.id.correct_answer2 == checkedId) {
question2 = true;
} else {
question2 = false;
}
}
});
RadioGroup rGroup3 = (RadioGroup) findViewById(R.id.third_question_group);
RadioButton checkedRadioButton3
= (RadioButton) rGroup3.findViewById(rGroup3.getCheckedRadioButtonId());
rGroup3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (R.id.correct_answer3 == checkedId) {
question3 = true;
} else {
question3 = false;
}
}
});
}
//used to diplay the players previous score on screen.
public void displayScore() {
TextView scoreView = (TextView) findViewById(R.id.score_keeper);
score = correct + wrong;
scoreView.setText(Integer.toString(correct));
}
//used to submit results with button and tally score.
public void submitResults(View view){
question1();
question2();
question3();
displayScore();
score= 0;
correct = 0;
wrong = 0;
}
//selector for radioGroup1 check if id matches correct answer.
//submit results calls this to find out if the answer was correct for question 1 to use for tally.
public void question1() {
if(question1) {
correct++;
} else {
wrong--;
}
}
//submit results calls this to find out if the answer was correct for question 2 to use for tally.
public void question2() {
if(question2) {
correct++;
} else {
wrong--;
}
}
//submit results calls this to find out if the answer was correct for question 3 to use for tally.
public void question3() {
if(question3) {
correct++;
} else {
wrong--;
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.example.samuel.trivaapp.MainActivity">
<TextView
android:id="@+id/header_logo"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_margin="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/player_name"
android:layout_below="@+id/header_logo"
android:inputType="textCapWords"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/enter_player_name"
android:layout_margin="8dp"/>
<LinearLayout
android:id="@+id/score_board"
android:layout_below="@+id/player_name"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score:"
android:textStyle="bold"
android:layout_marginLeft="8dp"
/>
<TextView
android:id="@+id/score_keeper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_marginLeft="8dp"/>
</LinearLayout>
<!--questions scroll view.-->
<ScrollView
android:id="@+id/question_scroll_view"
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/score_board">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp">
<RadioGroup
android:id="@+id/first_question_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Is this question 1? "/>
<RadioButton
android:id="@+id/correct_answer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 4" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="@+id/test2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/test"
android:layout_marginBottom="16dp">
<RadioGroup
android:id="@+id/second_question_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Is this question 2? "/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 1" />
<RadioButton
android:id="@+id/correct_answer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 4" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="@+id/test3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/test2"
android:layout_marginBottom="16dp">
<RadioGroup
android:id="@+id/third_question_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Is this question 3? "/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 2" />
<RadioButton
android:id="@+id/correct_answer3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 4" />
</RadioGroup>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
android:onClick="submitResults"
android:layout_below="@id/test3"
android:layout_centerInParent="true"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
好的,我相信我想出了我想做的事情。如果其他人有这个问题或类似的问题,使用多个 RadioGroups 我希望这会有所帮助。
https://github.com/raregamer/TrivaApp1/tree/trivia2/app/src/main/java/com/example/samuel/trivaapp
您好,我已经尝试了一段时间并研究了很多理论和堆栈答案。虽然我只是不知道如何将下面的代码用于一个简单的琐事应用程序,使用 RadioGroup 中的 RadioButton,然后在选中时添加一个来评分。问题是,如果您一直按正确答案,分数只会增加。如果答案改变点相应调整,我想应用某种类型的切换。
我也想清理这段代码,以免重复。使用新的 class 或方法。虽然我不知道如何实现它。
主要
package com.example.samuel.trivaapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//Score keeper.
private int score = 0;
private int correct = 0;
private int wrong = 0;
// determines if the selected was right or wrong, then used to add up the score.
boolean question1 = false;
boolean question2 = false;
boolean question3 = false;
// private boolean question3 = false;
// private boolean question4 = false;
// private boolean question5 = false;
// private boolean question6 = false;
// private boolean question7 = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup rGroup1 = (RadioGroup) findViewById(R.id.first_question_group);
RadioButton checkedRadioButton1 = (RadioButton) rGroup1.findViewById(rGroup1.getCheckedRadioButtonId());
rGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (R.id.correct_answer1 == checkedId) {
question1 = true;
} else {
question1 = false;
}
}
});
RadioGroup rGroup2 = (RadioGroup) findViewById(R.id.second_question_group);
RadioButton checkedRadioButton2
= (RadioButton) rGroup2.findViewById(rGroup2.getCheckedRadioButtonId());
rGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (R.id.correct_answer2 == checkedId) {
question2 = true;
} else {
question2 = false;
}
}
});
RadioGroup rGroup3 = (RadioGroup) findViewById(R.id.third_question_group);
RadioButton checkedRadioButton3
= (RadioButton) rGroup3.findViewById(rGroup3.getCheckedRadioButtonId());
rGroup3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (R.id.correct_answer3 == checkedId) {
question3 = true;
} else {
question3 = false;
}
}
});
}
//used to diplay the players previous score on screen.
public void displayScore() {
TextView scoreView = (TextView) findViewById(R.id.score_keeper);
score = correct + wrong;
scoreView.setText(Integer.toString(correct));
}
//used to submit results with button and tally score.
public void submitResults(View view){
question1();
question2();
question3();
displayScore();
score= 0;
correct = 0;
wrong = 0;
}
//selector for radioGroup1 check if id matches correct answer.
//submit results calls this to find out if the answer was correct for question 1 to use for tally.
public void question1() {
if(question1) {
correct++;
} else {
wrong--;
}
}
//submit results calls this to find out if the answer was correct for question 2 to use for tally.
public void question2() {
if(question2) {
correct++;
} else {
wrong--;
}
}
//submit results calls this to find out if the answer was correct for question 3 to use for tally.
public void question3() {
if(question3) {
correct++;
} else {
wrong--;
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.example.samuel.trivaapp.MainActivity">
<TextView
android:id="@+id/header_logo"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_margin="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/player_name"
android:layout_below="@+id/header_logo"
android:inputType="textCapWords"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/enter_player_name"
android:layout_margin="8dp"/>
<LinearLayout
android:id="@+id/score_board"
android:layout_below="@+id/player_name"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score:"
android:textStyle="bold"
android:layout_marginLeft="8dp"
/>
<TextView
android:id="@+id/score_keeper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_marginLeft="8dp"/>
</LinearLayout>
<!--questions scroll view.-->
<ScrollView
android:id="@+id/question_scroll_view"
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/score_board">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp">
<RadioGroup
android:id="@+id/first_question_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Is this question 1? "/>
<RadioButton
android:id="@+id/correct_answer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 4" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="@+id/test2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/test"
android:layout_marginBottom="16dp">
<RadioGroup
android:id="@+id/second_question_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Is this question 2? "/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 1" />
<RadioButton
android:id="@+id/correct_answer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 4" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="@+id/test3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/test2"
android:layout_marginBottom="16dp">
<RadioGroup
android:id="@+id/third_question_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Is this question 3? "/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 2" />
<RadioButton
android:id="@+id/correct_answer3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 4" />
</RadioGroup>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
android:onClick="submitResults"
android:layout_below="@id/test3"
android:layout_centerInParent="true"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
好的,我相信我想出了我想做的事情。如果其他人有这个问题或类似的问题,使用多个 RadioGroups 我希望这会有所帮助。
https://github.com/raregamer/TrivaApp1/tree/trivia2/app/src/main/java/com/example/samuel/trivaapp