android - 当在同一 Activity 中再次使用 radio 组时,重置我选中的 radio 组

标签 android android-layout radio-group

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

这是我的 Activity ....

 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;
    }

    }

}


}

}

最佳答案

我认为你应该在 onPostExecute()

中清除对你的 RadioGroup 的检查
this.radioGroup.clearCheck();
this.radioGroup.setOnCheckedChangeListener(this);

关于android - 当在同一 Activity 中再次使用 radio 组时,重置我选中的 radio 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10060423/

相关文章:

android - 我可以通过蓝牙从设备获取 VendorID、ProductID 吗?

android - 对话框布局显示不正确

java - 是否有一个 Android 监听器用于切换列表上的 View 属性 boolean 值?

android - RadioGroup onCheckedChangeListener 出错

python - 一个窗口中的多个 QButtonGroup

java - 如何在 MPAndroidChart 中的条形图上正确添加标签

android - 为 Android ARMV7 编译 OpenSSL 1.1.0

javascript - PhoneGap 电子邮件插件

android - 对于不断变化的列表,最好的 View /Activity 类型是什么?

android - 在 Android 中使用 setError 检查 RadioGroup 空字段