开发者

Radio button cannot be unchecked

开发者 https://www.devze.com 2023-03-18 12:47 出处:网络
I have created a radio button in my app. Now when radio button isChecked I call a function, and it\'s working fine. But my problem is that once I checked the radio button then and again click of that

I have created a radio button in my app. Now when radio button isChecked I call a function, and it's working fine. But my problem is that once I checked the radio button then and again click of that radio button doesn't make it unchecked.

So, how that can be done?

My code:

super.onCreate(savedInstanceState);
    setContentView(R.layout.setting_frm);
    rb1=(RadioButton)findViewBy开发者_开发问答Id(R.id.option1);
    rb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton v, boolean arg1) {
            // TODO Auto-generated method stub
            if(rb1.isChecked() == true)
                t1.setText("Selected is : "+rb1.getText());
            if(rb1.isChecked() == false)
                t1.setText("Selected is : ");
        }
    });

    t1=(TextView)findViewById(R.id.TextView01);
}


You can't do that with a radio button. Go for CheckBox. Try this


you can use boolean Flag option ..here is the code

boolean flag = false;

rb.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if(flag){

                    rb.setChecked(false);
                    flag = false;
                }
                else{

                    rb.setChecked(true);
                    flag = true;
                }

            }
        });
0

精彩评论

暂无评论...
验证码 换一张
取 消