Java 如何在数组中使用单选按钮?
我有兴趣为Android开发一款包含30个个性特征问题的应用程序,每个问题都有两种选择。我曾想过使用RadioButton,但由于有30个问题,我不想一次将它们全部显示在屏幕上,我只想显示一个问题和两个备选方案,其中一个备选方案的每个选择都称为另一个问题 是否可以在不创建30个活动的情况下执行此操作 我看到可能会使用数组,但我不知道如何从一个问题运行到另一个问题Java 如何在数组中使用单选按钮?,java,android,arrays,radio-button,Java,Android,Arrays,Radio Button,我有兴趣为Android开发一款包含30个个性特征问题的应用程序,每个问题都有两种选择。我曾想过使用RadioButton,但由于有30个问题,我不想一次将它们全部显示在屏幕上,我只想显示一个问题和两个备选方案,其中一个备选方案的每个选择都称为另一个问题 是否可以在不创建30个活动的情况下执行此操作 我看到可能会使用数组,但我不知道如何从一个问题运行到另一个问题 非常感谢你 此代码实现了所需的功能。这是非常基本的,可以根据您的需要进行改进。只需将此活动和布局添加到项目中 Java文件:此代码使用
非常感谢你 此代码实现了所需的功能。这是非常基本的,可以根据您的需要进行改进。只需将此活动和布局添加到项目中 Java文件:此代码使用单个布局,并在单选按钮单击时为下一个问题重新创建视图
public class QuestionsActivity extends AppCompatActivity implements
RadioGroup.OnCheckedChangeListener{
LinkedHashMap<String,RadioGroup> questionList;
LinkedHashMap<String,String> answerList;
ArrayList<String> keys=new ArrayList<>();
int keyCounter=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questions);
questionList=new LinkedHashMap<>(); //This Map contains all the questions with two radio button (options)
answerList=new LinkedHashMap<>(); //This Map will contain question along with selected answer.
initQuestions(); //This method will add 30 questions with options
keys.addAll(questionList.keySet());
showQuestions(keys.get(keyCounter));//This method will show the first question
}
private void showQuestions(String key) {
TextView textView=(TextView)findViewById(R.id.tv_question);
textView.setText(key);
LinearLayout layout =(LinearLayout)findViewById(R.id.questionsLayout);
layout.removeAllViews();
RadioGroup rg=questionList.get(key);
rg.setOrientation(LinearLayout.HORIZONTAL);
layout.addView(rg);
rg.setOnCheckedChangeListener(this);
}
private void initQuestions() {
for (int i = 1; i <=3; i++) {
RadioGroup rg=new RadioGroup(this);
RadioButton rb1 =new RadioButton(this);
rb1.setText("Q "+i+" RadioButton 1");
RadioButton rb2 =new RadioButton(this);
rb2.setText("Q "+i+" RadioButton 2");
rg.addView(rb1);rg.addView(rb2);
questionList.put("Question "+i,rg);
}
}
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
RadioButton rb=(RadioButton) findViewById(group.getCheckedRadioButtonId());
if(keyCounter<questionList.size()) {
answerList.put(keys.get(keyCounter),rb.getText().toString()); // Putting the question and selected answer to 'answerList' map.
keyCounter++;
if(keyCounter<questionList.size()) {
showQuestions(keys.get(keyCounter));// showing the next question.
}
}else {
Toast.makeText(this, "You've answered all the questions.", Toast.LENGTH_SHORT).show();
}
for (String s : answerList.keySet()) {
System.out.println("Q--> "+s+", A--> "+answerList.get(s)); // Here you can see all the questions and selected answers on your logs(AndroidMonitor).
}
}
}
公共类问题活动扩展了AppCompatActivity实现
RadioGroup.OnCheckedChangeListener{
LinkedHashMap问题列表;
LinkedHashMap回答列表;
ArrayList键=新的ArrayList();
int键计数器=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_问题);
questionList=new LinkedHashMap();//此映射包含带有两个单选按钮(选项)的所有问题
answerList=new LinkedHashMap();//此映射将包含问题和所选答案。
initQuestions();//此方法将添加30个带选项的问题
addAll(questionList.keySet());
showQuestions(keys.get(keyCounter));//此方法将显示第一个问题
}
私有void显示问题(字符串键){
TextView TextView=(TextView)findViewById(R.id.tv\u问题);
textView.setText(键);
LinearLayout布局=(LinearLayout)findViewById(R.id.questionsLayout);
layout.removeallview();
RadioGroup rg=questionList.get(键);
rg.设置方向(线性布局。水平);
布局。添加视图(rg);
rg.setOnCheckedChangeListener(此);
}
私人问题(){
对于(int i=1;i为什么你要复制自己2次?我不这么认为创建30个活动是好的,为什么你不一次显示一个问题,在用户选择正确答案后更改问题?这样会更有效。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
tools:context="edios.endlessscrollrecycler.QuestionsActivity">
<TextView
android:id="@+id/tv_question"
android:layout_marginTop="10dp"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:orientation="horizontal"
android:id="@+id/questionsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"></LinearLayout>
</LinearLayout>