我正在做一个测试应用程序,在那里我有20个选择题。我必须将选中的单选按钮值存储到字符串数组中,以便在应用程序后面使用用户选择的答案。下面是获取单选按钮文本的代码。现在,我如何将这20个选择保存到一个数组中?
if (rg.getCheckedRadioButtonId() != -1) {
RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
String ansText = uans.getText().toString();
}
发布于 2016-06-07 07:21:08
您可以将它们保存在字符串的arrayList中,其中问题的索引也是数组中答案的索引。
ArrayList<String> answersStringArray= new ArrayList<String>();
if (rg.getCheckedRadioButtonId() != -1) {
RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
String ansText = uans.getText().toString();
answersStringArray.add(ansText);
}
发布于 2016-06-07 07:23:53
你能拿到quesText吗?
尝试使用HashMap
HashMap<String,String> mMap = new HashMap<>();
if (rg.getCheckedRadioButtonId() != -1) {
RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
String ansText = uans.getText().toString();
mMap.put(quesText,ansText);
}
如果你只保存答案,你可以使用ArrayList
ArrayList<String> mArrayList = new ArrayList<>();
if (rg.getCheckedRadioButtonId() != -1) {
RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
String ansText = uans.getText().toString();
mArrayList.add(ansText);
}
https://stackoverflow.com/questions/37672319
复制相似问题