当在同一活动中再次使用radiogroup时,重置我检查的无线电组

时间:2012-04-08 04:40:00

标签: android android-layout radio-group

我正在制作一个测验应用程序...我通过“id”访问问题...每次回答问题(选中任何单选按钮)时,id都会递增并调用doInBackground()函数加载下一个问题....但是当下一个问题被加载时我希望我的radiogroup完全刷新(没有收音机检查,原始的白色文本)...怎么做????

这是我的活动......

 public class JsonDemo extends Activity 
 {
JSONObject json;
HttpClient client;
TextView q_desc,c_or_w,id_check;
RadioButton rb_a,rb_b,rb_c,rb_d;
RadioGroup rg;
String ans;
int id=1;
int score=0;




@Override
protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);

    setContentView(R.layout.result_json);

    q_desc=(TextView)findViewById(R.id.q_desc);
    c_or_w=(TextView)findViewById(R.id.corr_incorrect);
    id_check=(TextView)findViewById(R.id.id_check);
    rg=(RadioGroup)findViewById(R.id.rg_option);
    rb_a=(RadioButton)findViewById(R.id.opt_a);
    rb_b=(RadioButton)findViewById(R.id.opt_b);
    rb_c=(RadioButton)findViewById(R.id.opt_c);
    rb_d=(RadioButton)findViewById(R.id.opt_d);

    client=new DefaultHttpClient();


    new Read().execute(Integer.toString(id));

}
 public  JSONObject getData(String id)throws     ClientProtocolException,IOException,JSONException
   {
     String url = "http://10.0.2.2:7001/proj/json.jsp";
     HttpPost post = new HttpPost(url);
     List<NameValuePair> pairs = new ArrayList<NameValuePair>();
     pairs.add(new BasicNameValuePair("id",id));
     post.setEntity(new UrlEncodedFormEntity(pairs));

    HttpResponse r=client.execute(post);
    int status=r.getStatusLine().getStatusCode();

    if(status == 200)
    {
        HttpEntity e=r.getEntity();
        String data=EntityUtils.toString(e);
        JSONObject last=new JSONObject(data);
        return last;


    }
    else
    {
        Toast.makeText(JsonDemo.this, "error", Toast.LENGTH_SHORT);
        return null;
    }

}



 public class Read extends AsyncTask<String,Integer,JSONObject> implements     OnCheckedChangeListener
{
ProgressDialog dialog = ProgressDialog.show(JsonDemo.this, "", "Loading Question,   Please wait...");

@Override
protected void onPreExecute() {


    dialog.show();

}

@Override
protected void onProgressUpdate(Integer... values) {
    // TODO Auto-generated method stub
    rg.clearCheck();
}

@Override
protected void onPostExecute(JSONObject result) {
    // TODO Auto-generated method stub
    try {
        String desc=json.getString("desc");
        String option_a=json.getString("a");
        String option_b=json.getString("b");
        String option_c=json.getString("c");
        String option_d=json.getString("d");
        ans=json.getString("ans");
        q_desc.setText(desc);
        rb_a.setText(option_a);
        rb_b.setText(option_b);
        rb_c.setText(option_c);
        rb_d.setText(option_d);
        rg.clearFocus();
        if(dialog!=null)
        {
            dialog.dismiss();
        }


        rg.setOnCheckedChangeListener(this);

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 }

 @Override
 protected JSONObject doInBackground(String... params) {
    try {
        json=getData(params[0]);
        return json;
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
 }

 public void onCheckedChanged(RadioGroup rg, int checkedId) {

    switch(checkedId)
    {
    case R.id.opt_a:
    {
        if(ans.equalsIgnoreCase("a"))
        {

            id=id+1;
            c_or_w.setText("Correct");
            id_check.setText(Integer.toString(id)); 
            rb_a.setTextColor(Color.parseColor("#33ff99"));
            if(id>10)
            {

            }
            else{
                score=score+1;
            new Read().execute(Integer.toString(id));
            }
        }
        else
        {
            id=id+1;
            c_or_w.setText("InCorrect");
            id_check.setText(Integer.toString(id));
            if(id>10)
            {

            }
            else{
            new Read().execute(Integer.toString(id));
            }

        }
        break;

    }
    case R.id.opt_b:
    {
        if(ans.equalsIgnoreCase("b"))
        {

            id=id+1;
            c_or_w.setText("Correct");
            id_check.setText(Integer.toString(id));
            rb_b.setTextColor(R.color.green);
            if(id>10)
            {

            }
            else{
                score=score+1;
            new Read().execute(Integer.toString(id));
            }   
        }
        else
        {
            id=id+1;
            c_or_w.setText("InCorrect");
            id_check.setText(Integer.toString(id));
            if(id>10)
            {

            }
            else{

            new Read().execute(Integer.toString(id));
            }   
            }
        break;
    }
    case R.id.opt_c:
    {
        if(ans.equalsIgnoreCase("c"))
        {

            id=id+1;
            rb_a.setTextColor(666);
            c_or_w.setText("Correct");
            id_check.setText(Integer.toString(id));         
            if(id>10)
            {

            }
            else{
                score=score+1;
            new Read().execute(Integer.toString(id));
            }
        }
        else
        {
            id=id+1;
            c_or_w.setText("InCorrect");
            id_check.setText(Integer.toString(id));
            if(id>10)
            {

            }
            else{
            new Read().execute(Integer.toString(id));
            }

        }
        break;
    }
    case R.id.opt_d:
    {
        if(ans.equalsIgnoreCase("d"))
        {

            id=id+1;
            c_or_w.setText("Correct");
            id_check.setText(Integer.toString(id)); 
            if(id>10)
            {

            }
            else{
                score=score+1;
            new Read().execute(Integer.toString(id));
            }
        }
        else
        {
            id=id+1;
            c_or_w.setText("InCorrect");
            id_check.setText(Integer.toString(id));
            if(id>10)
            {

            }
            else{

            new Read().execute(Integer.toString(id));
            }

        }
        break;
    }

    }

}


}

}

2 个答案:

答案 0 :(得分:10)

我认为您应该清楚检查RadioGroup

中的onPostExecute()
this.radioGroup.clearCheck();
this.radioGroup.setOnCheckedChangeListener(this);

答案 1 :(得分:0)

如果有人仍然试图在不必创建新活动的情况下找到类似问题的解决方案,请在每次重新使用单选按钮组时尝试设置每个单选按钮 ID。

RadioButton radioButton = findViewById(R.id.radio_button);
radioButton.setId(0);