我有兴趣为Android制作一个包含30个人格档案问题的APP,每个问题都有两个选择。我想过使用RadioButton,但由于有30个问题,我不想一次将它们全部包含在屏幕上,我想只显示一个带有两个选项的问题,并且每个选项中的一个选项已经被称为另一个问题
是否可以在不创建30个活动的情况下执行此操作?
我看到有可能做数组,但我不知道如何将它从一个问题运行到另一个问题。
非常感谢!!!
答案 0 :(得分:0)
此代码实现了所需的功能。它非常基础,可以根据您的需要进行改进。只需将此活动和布局添加到项目中即可。
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).
}
}
}
布局文件:此文件包含一个用于显示问题的textView和一个包含RadioGroup的线性布局
<?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>