android未经检查的单选按钮已经检查过

时间:2015-05-07 06:33:07

标签: android radio-button unchecked

grid.setCellCssStyles(key, hash)
我有一个广播组 我有2个单选按钮

现在如果选择单选按钮1,如果我点击相同的单选按钮(单选按钮1),则应该取消选中它。

它应该用作切换

3 个答案:

答案 0 :(得分:1)

This should serve the purpose.

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
    @Override
    public void onCheckedChanged(RadioGroup arg0, int id)
    {
    RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(id);

    boolean isChecked = checkedRadioButton.isChecked();

    if (isChecked)
    {
    Toast.makeText(getApplicationContext(), 
               checkedRadioButton.getText(),Toast.LENGTH_LONG).show();
    radioButton = (String) checkedRadioButton.getText();
    checkedRadioButton.setChecked(false);
    }
    else
        checkedRadioButton.setChecked(true);
    }});

Also, don't forget to initialize RadioGroup

答案 1 :(得分:1)

是..单选按钮可以像复选框一样工作

首先声明标志“false”

private boolean flagmale = false;
private boolean flagfemale = false;


rdbtnMale.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (rdbtnMale.isChecked()) {
                if (!flagmale) {
                    rdbtnMale.setChecked(true);
                    rdbtnFemale.setChecked(false);
                    flagmale = true;
                    flagfemale = false;
                } else {
                    flagmale = false;
                    rdbtnMale.setChecked(false);
                    rdbtnFemale.setChecked(false);
                }
            }
        }
    });


    rdbtnFemale.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (rdbtnFemale.isChecked()) {
                if (!flagfemale) {
                    rdbtnFemale.setChecked(true);
                    rdbtnMale.setChecked(false);
                    flagfemale = true;
                    flagmale = false;
                } else {
                    flagfemale = false;
                    rdbtnFemale.setChecked(false);
                    rdbtnMale.setChecked(false);
                }
            }
        }
    });

这也可以用一个标志..

答案 2 :(得分:0)

使用以下方法以编程方式在警告框中显示单选按钮

 Private void SelectLanguageMethod(){


    // Strings to Show In Dialog with Radio Buttons
            final CharSequence[] items = {"English"," Spanish "};

            // Creating and Building the Dialog
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Select The Language");
            builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {

            //Your code here

         }
            });
            levelDialog = builder.create();
            levelDialog.show();
        }