我是安卓新手。。
我在取消选中组中的单选按钮时遇到问题....
我的问题是......
我正在开发一个基于测验的应用程序。当用户打开此应用程序时,将有 1 个任务和 4 个选择(单选按钮)单选按钮将被取消选中,但当用户回答 1 个任务并且当他进行下一个任务时,将选中单选按钮。我想取消选中/重置每个任务的单选按钮。我该怎么做?
我面临的另一个问题是......如果假设用户选择了第一个按钮,他想再次更改选项,所以他选择了另一个按钮,那时他不应该选择..
如何实施?
任何帮助,将不胜感激。
protected void onPostExecute(String file_url) { pDialog.dismiss(); ques1=new ArrayList<String>(new ArrayList<String>(ques1)); // j=0; TextView txtque = (TextView) findViewById(R.id.que_txt); txtque.setText(ques1.get(j)); answ1=new ArrayList<String>(new ArrayList<String>(answ1)); btn_practice1.setText(answ1.get(0)); btn_practice2.setText(answ1.get(1)); btn_practice3.setText(answ1.get(2)); btn_practice4.setText(answ1.get(3)); btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radioButton = (RadioButton) findViewById(checkedId); // error // is here 205 line.. String temp = radioButton.getText().toString(); crrtans=new ArrayList<String>(new ArrayList<String>(crrtans)); if (temp.equals(crrtans.get(l))){ TextView txtRadio = (TextView) findViewById(R.id.rdtxt); txtRadio.setText("" + radioButton.getText() + " IS CORRECT"); txtRadio.setTextColor(Color.parseColor("#008000")); }else{ //RadioButton radioButton1 = (RadioButton) findViewById(checkedId); // Toast.makeText(Question.this, "" + radioButton.getText(), 2000).show(); TextView txtRadio = (TextView) findViewById(R.id.rdtxt); txtRadio.setText("" + radioButton.getText() + "is INCORRECT"); txtRadio.setTextColor(Color.parseColor("#FF0000")); } } }); Button nextBtn = (Button) findViewById(R.id.nxt_btn); nextBtn.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ // btn_practicerg.clearCheck(); if (j == ques1.size() -1) { //finish(); showAlert1(); } else{ ++j; TextView txtque = (TextView) findViewById(R.id.que_txt); txtque.setText(ques1.get(j)); ++k; btn_practice1.setText(answ1.get((k*4)+0)); btn_practice2.setText(answ1.get((k*4)+1)); btn_practice3.setText(answ1.get((k*4)+2)); btn_practice4.setText(answ1.get((k*4)+3)); } btn_practicerg.clearCheck(); btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radioButton = (RadioButton) findViewById(checkedId); String temp = radioButton.getText().toString(); l++; if (temp.equals(crrtans.get(l))){ TextView txtRadio = (TextView) findViewById(R.id.rdtxt); txtRadio.setText("" + radioButton.getText() + " IS CORRECT"); txtRadio.setTextColor(Color.parseColor("#008000")); }else{ //RadioButton radioButton1 = (RadioButton) findViewById(checkedId); // Toast.makeText(Question.this, "" + radioButton.getText(), 2000).show(); TextView txtRadio = (TextView) findViewById(R.id.rdtxt); txtRadio.setText("" + radioButton.getText() + " IS INCORRECT"); txtRadio.setTextColor(Color.parseColor("#FF0000")); } } }); } }); Button previousbtn = (Button) findViewById(R.id.prv_btn); previousbtn.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ if (j <= 0) { Toast.makeText(Question.this, "First Question",Toast.LENGTH_SHORT).show(); } else { --j; TextView txtque = (TextView) findViewById(R.id.que_txt); txtque.setText(ques1.get(j)); --k; btn_practice1.setText(answ1.get((k*4)+0)); btn_practice2.setText(answ1.get((k*4)+1)); btn_practice3.setText(answ1.get((k*4)+2)); btn_practice4.setText(answ1.get((k*4)+3)); } btn_practicerg.clearCheck(); btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radioButton = (RadioButton) findViewById(checkedId); String temp = radioButton.getText().toString(); l--; if (temp.equals(crrtans.get(l))){ TextView txtRadio = (TextView) findViewById(R.id.rdtxt); txtRadio.setText("" + radioButton.getText() + " IS CORRECT"); txtRadio.setTextColor(Color.parseColor("#008000")); }else{ //RadioButton radioButton1 = (RadioButton) findViewById(checkedId); // Toast.makeText(Question.this, "" + radioButton.getText(), 2000).show(); TextView txtRadio = (TextView) findViewById(R.id.rdtxt); txtRadio.setText("" + radioButton.getText() + " IS INCORRECT"); txtRadio.setTextColor(Color.parseColor("#FF0000")); } } }); } }); }
日志猫
E/AndroidRuntime(893): FATAL EXCEPTION: main
E/AndroidRuntime(893): java.lang.ArrayIndexOutOfBoundsException
E/AndroidRuntime(893): at java.util.ArrayList.get(ArrayList.java:313)
E/AndroidRuntime(893): at
com.example.finalpractice.Question$LoadQuestions$2$1.onCheckedChanged(Question.java:243)
E/AndroidRuntime(893): at
android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
E/AndroidRuntime(893): at android.widget.RadioGroup.access$600(RadioGroup.java:52)
E/AndroidRuntime(893): at
android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342)
E/AndroidRuntime(893): at
android.widget.CompoundButton.setChecked(CompoundButton.java:127)
E/AndroidRuntime(893): at
android.widget.CompoundButton.toggle(CompoundButton.java:86)
E/AndroidRuntime(893): at android.widget.RadioButton.toggle(RadioButton.java:72)
E/AndroidRuntime(893): at
android.widget.CompoundButton.performClick(CompoundButton.java:98)
E/AndroidRuntime(893): at android.view.View$PerformClick.run(View.java:9080)
E/AndroidRuntime(893): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(893): at android.os.Handler.dispatchMessage(Handler.java:92)