我正在使用服务器数据库在android中开发一个竞赛(多选类型)。 有很多问题有多个选择答案(每个4个)。通过以下代码创建问题
try {
JSONArray array = (JSONArray) new JSONTokener(questions).nextValue();
TextView active=(TextView)findViewById(R.id.active);
active.setText(description);
active.setVisibility(View.VISIBLE);
LinearLayout ll=(LinearLayout)findViewById(R.id.questions_lay);
for(int i=0;i<array.length();i++) {
JSONObject question = array.getJSONObject(i);
Integer id=question.getInt("id");
TextView title = new TextView(this);
title.setText(question.getString("ques"));
title.setTextColor(Color.parseColor("#FFFFFF"));
ll.addView(title);
final RadioButton[] rb = new RadioButton[4];
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.VERTICAL);
rb[0] = new RadioButton(this);
rb[0].setText(question.getString("opt_a"));
rb[1] = new RadioButton(this);
rb[1].setText(question.getString("opt_b"));
rb[2] = new RadioButton(this);
rb[2].setText(question.getString("opt_c"));
rb[3] = new RadioButton(this);
rb[3].setText(question.getString("opt_d"));
rg.addView(rb[0]);
rg.addView(rb[1]);
rg.addView(rb[2]);
rg.addView(rb[3]);
ll.addView(rg);
}
ll.setVisibility(View.VISIBLE);
overLay.setVisibility(View.GONE);
} catch (JSONException e) {
// handle JSON parsing exceptions...
Toast.makeText(ContestActivity.this,"Error Occured ! Please try again.",Toast.LENGTH_SHORT).show();
cd.goHome(ContestActivity.this);
e.printStackTrace();
}
现在点击按钮我需要将答案发送到服务器。我是android新手,我怀疑是
提前致谢
答案 0 :(得分:1)
确定首先在布局中创建一个按钮:
1)你的问题在这里?
第4项
点击我按钮
这是你的布局:
现在,如果你想将一些值设置为单选按钮,然后通过setTag或setId设置它,然后在代码中设置:
final RadioButton[] rb = new RadioButton[4];
rb[1].setTag("some value");
rb[1].setId(1);
int arrayQID[] = new Int[array.length()];
然后单击按钮的单击侦听器
int selectedId = rg.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) layout.findViewById(selectedId);
String value = (String)radioButton.getTag();
int value2 = radioButton.getId();
for(int i=0;i<array.length();i++) {
JSONObject question = array.getJSONObject(i);
Integer id=question.getInt("id");
TextView title = new TextView(this);
title.setText(question.getString("ques"));
title.setTextColor(Color.parseColor("#FFFFFF"));
ll.addView(title);
final RadioButton[] rb = new RadioButton[4];
RadioGroup rg = new RadioGroup(this);
rg.setId(qid); // i suggest set your question id here instead of loop count
rg.setTag("some value"); // Some value
arrayQID[i] = qid
rg.setOrientation(RadioGroup.VERTICAL);
rb[0] = new RadioButton(this);
rb[0].setText(question.getString("opt_a"));
rb.[0].setId(id); //id should be unique (not the same for each radioGroup [1]
rb.[0].setTag("qid"+"opt_a");
rb[1] = new RadioButton(this);
rb[1].setText(question.getString("opt_b"));
rb[2] = new RadioButton(this);
rb[2].setText(question.getString("opt_c"));
rb[3] = new RadioButton(this);
rb[3].setText(question.getString("opt_d"));
rg.addView(rb[0]);
rg.addView(rb[1]);
rg.addView(rb[2]);
rg.addView(rb[3]);
ll.addView(rg);
}
[1]在这里你可以通过组合问题id和选项id来构建你的逻辑,即(qid + opt_a_id)= id并解码它gid = radioButtonid - opt_a_id这样的东西在镜头中你做了一个唯一的id可以解码,以便你知道这个radioButton属于这个问题 类似地,您可以使用标签在findViewWithTag(“qid”+“opt_a”)的帮助下获取视图对象; 你可以这样做
RadioButton rb = layout.findViewWithTag("qid"+"opt_a");
要获得所有问题和答案,您可以设置foreach循环:
for(int qid : arrayQID) {
RadioButton rb = layout.findViewWithTag("qid"+"opt_a"); // OR
RadioButton rb = layout.findViewById(id); // Unique id ie (qid + opt_a_Id)
}