在一个 activity 内使用单选按钮
Use RadioButtons within one activity
我正在制作一个测验应用程序,其中有一些问题:
当用户单击 NEXT QUESTION
时,将显示另一个问题。
我做了现在的代码:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.number_1:
if (checked)
point = 0;
Toast.makeText(this, ""+ point,
Toast.LENGTH_LONG).show();
break;
case R.id.number_2:
if (checked)
point = 1;
Toast.makeText(this, ""+ point,
Toast.LENGTH_LONG).show();
break;
case R.id.number_3:
if (checked)
point = 2;
Toast.makeText(this, ""+ point,
Toast.LENGTH_LONG).show();
break;
case R.id.number_4:
if (checked)
point = 3;
Toast.makeText(this, ""+ point,
Toast.LENGTH_LONG).show();
break;
}
}
如何在单击 NEXT QUESTION
时发生这种情况,在同一 activity 中弹出另一个问题?
你有问题TextView,只需使用questionTextView.setText(newQuestion)
将所有单选按钮设置为 false/unselected idk 是正确的。
但最好在您的 activity 中放置一个 FrameLayout 并在您的用户每次回答问题并构建一个新的 View 时调用一个新的 QuestionFragment ,您的控制器将更加独立。
你可以这样做
public void nextQuestion(View view){
questionTextView.setText(gtString(R.string.nextQuestion));
checkBox1.setText("option1");
checkBox2.setText("option2");
checkBox3.setText("option3");
checkBox4.setText("option4");
}
将以上所有视图名称替换为您的名称
1. 如果您使用的是 RadioGroup
,那么当 NextQestion
按钮首先被点击时 clear
[=14] 的状态=]调用方法RadioGroup.clearCheck()
。
2. 从您的 database
或其他地方获取下一个 question
并将值设置为您的 TextView
和 RadioButtons
.
举个例子:
public class Main2Activity extends AppCompatActivity {
TextView textQuestion;
RadioGroup radioGroup;
RadioButton radioButton1,radioButton2,radioButton3;
Button buttonNextQuestion;
int questionNo = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textQuestion = (TextView) findViewById(R.id.text_question);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
radioButton3 = (RadioButton) findViewById(R.id.radioButton3);
buttonNextQuestion = (Button) findViewById(R.id.button_next_question);
buttonNextQuestion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
questionNo++;
loadNextQuestion();
}
});
}
public void loadNextQuestion() {
String question, option1, option2, option3, option4;
// Get question and options from database or from other place
// and store into >> question, option1, option2, option3, option4
// Update UI
// Clear radio button state
radioGroup.clearCheck();
// Set new question and its options to view
textQuestion.setText(question);
radioButton1.setText(option1);
radioButton2.setText(option2);
radioButton3.setText(option3);
radioButton4.setText(option4);
}
}
我正在制作一个测验应用程序,其中有一些问题:
当用户单击 NEXT QUESTION
时,将显示另一个问题。
我做了现在的代码:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.number_1:
if (checked)
point = 0;
Toast.makeText(this, ""+ point,
Toast.LENGTH_LONG).show();
break;
case R.id.number_2:
if (checked)
point = 1;
Toast.makeText(this, ""+ point,
Toast.LENGTH_LONG).show();
break;
case R.id.number_3:
if (checked)
point = 2;
Toast.makeText(this, ""+ point,
Toast.LENGTH_LONG).show();
break;
case R.id.number_4:
if (checked)
point = 3;
Toast.makeText(this, ""+ point,
Toast.LENGTH_LONG).show();
break;
}
}
如何在单击 NEXT QUESTION
时发生这种情况,在同一 activity 中弹出另一个问题?
你有问题TextView,只需使用questionTextView.setText(newQuestion)
将所有单选按钮设置为 false/unselected idk 是正确的。
但最好在您的 activity 中放置一个 FrameLayout 并在您的用户每次回答问题并构建一个新的 View 时调用一个新的 QuestionFragment ,您的控制器将更加独立。
你可以这样做
public void nextQuestion(View view){
questionTextView.setText(gtString(R.string.nextQuestion));
checkBox1.setText("option1");
checkBox2.setText("option2");
checkBox3.setText("option3");
checkBox4.setText("option4");
}
将以上所有视图名称替换为您的名称
1. 如果您使用的是 RadioGroup
,那么当 NextQestion
按钮首先被点击时 clear
[=14] 的状态=]调用方法RadioGroup.clearCheck()
。
2. 从您的 database
或其他地方获取下一个 question
并将值设置为您的 TextView
和 RadioButtons
.
举个例子:
public class Main2Activity extends AppCompatActivity {
TextView textQuestion;
RadioGroup radioGroup;
RadioButton radioButton1,radioButton2,radioButton3;
Button buttonNextQuestion;
int questionNo = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textQuestion = (TextView) findViewById(R.id.text_question);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
radioButton3 = (RadioButton) findViewById(R.id.radioButton3);
buttonNextQuestion = (Button) findViewById(R.id.button_next_question);
buttonNextQuestion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
questionNo++;
loadNextQuestion();
}
});
}
public void loadNextQuestion() {
String question, option1, option2, option3, option4;
// Get question and options from database or from other place
// and store into >> question, option1, option2, option3, option4
// Update UI
// Clear radio button state
radioGroup.clearCheck();
// Set new question and its options to view
textQuestion.setText(question);
radioButton1.setText(option1);
radioButton2.setText(option2);
radioButton3.setText(option3);
radioButton4.setText(option4);
}
}