Android的单选按钮,不选中 [英] Android radio button uncheck

查看:746
本文介绍了Android的单选按钮,不选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该应用程序是用每组8个按钮16单选按钮组一个步进音序器应用程序。它完美的作品,除了一次组有选择的,我不能把它关掉,除非我用清晰的按钮,我创建了以清除所有radiogroups一个按钮。我想补充一些code,说当一个选定的单选按钮会再次选择它只是关闭就像一个开关。我试着用切换但其他问题出现与该方法。下面是两次尝试但都只是阻止我使用按钮

 最后RadioGroup中radioGroup1 =(RadioGroup中)findViewById(R.id.RadioGroup1);
     单选D1 =(单选)findViewById(R.id.RadioButtonD1);     按钮D1 =(按钮)findViewById(R.id.RadioButtonD1);
    D1.setOnClickListener(新View.OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            PdBase.sendFloat(D1,74);
            INT selectedTypeId = radioGroup1.getCheckedRadioButtonId();
            单选D1 =(单选)findViewById(selectedTypeId);
            如果(D1!= NULL)//如果没有一个单选按钮被选中这将是空
                   radioGroup1.clearCheck();
            PdBase.sendFloat(D1,0);
        }
    });单选lC1的=(单选)findViewById(R.id.RadioButtonlowC1);
        lC1.setOnClickListener(新View.OnClickListener(){            公共无效的onClick(视图v){                 INT selectedTypeId = radioGroup1.getCheckedRadioButtonId();                单选lC1的=(单选)findViewById(R.id.RadioButtonlowC1);
                如果(selectedTypeId == -1){
                PdBase.sendFloat(lC1的,72);
                }
                否则,如果(selectedTypeId == R.id.RadioButtonlowC1){
                       radioGroup1.clearCheck();
                        PdBase.sendFloat(lC1的,0);                }
            }
        });


解决方案

最近我同样需要 - 有显示所选的项目可以通过再次点击它被取消单选按钮组。我发现我无法做到,使用监听器,但我的的能够使用自定义做单选,像这样...

 公共类ToggleableRadioButton扩展单选{
    //实施必要的构造    @覆盖
    公共无效切换(){
        如果(器isChecked()){
            如果(的getParent()的instanceof RadioGroup中){
                ((RadioGroup中)的getParent())clearCheck();
            }
        }其他{
            setChecked(真);
        }
    }
}

注意,按钮切换不同的方式取决于其当前状态 - 即调用 setChecked(真)按钮与调用 clearCheck()上的组。如果 setChecked()在这两种情况时,刚取消,不能立即重新选择一个按钮 - 在逻辑 RadioGroup中似乎立即取消选择。

要使用此按钮,只需更换你的<具有&LT 标签; your.package.ToggleableRadioButton> 在布局XML。

The application is a step sequencer application with 16 radio groups with 8 buttons in each group. It works perfectly except once a group has a button selected I cant turn it off unless I use the clear button I have created to clear all radiogroups. What I would like to add is some code that says when a selected radio button is selected again it simply turns off like a toggle. I tried using toggles but then other issues arose with that method. Below are two attempts but both simply stops me using the button

     final RadioGroup radioGroup1 = (RadioGroup)findViewById(R.id.RadioGroup1);
     RadioButton D1 = (RadioButton)findViewById(R.id.RadioButtonD1);

     Button D1 = (Button)findViewById(R.id.RadioButtonD1);
    D1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            PdBase.sendFloat("D1", 74);
            int selectedTypeId = radioGroup1.getCheckedRadioButtonId();
            RadioButton D1 = (RadioButton) findViewById(selectedTypeId);
            if(D1 != null) // This will be null if none of the radio buttons are selected
                   radioGroup1.clearCheck(); 
            PdBase.sendFloat("D1", 0);
        }
    });

RadioButton lC1 = (RadioButton)findViewById(R.id.RadioButtonlowC1);
        lC1.setOnClickListener(new View.OnClickListener() {



            public void onClick(View v) {

                 int selectedTypeId = radioGroup1.getCheckedRadioButtonId();

                RadioButton lC1 = (RadioButton) findViewById(R.id.RadioButtonlowC1);
                if (selectedTypeId == -1){
                PdBase.sendFloat("lC1", 72);
                }
                else if (selectedTypeId == R.id.RadioButtonlowC1) {
                       radioGroup1.clearCheck(); 
                        PdBase.sendFloat("lC1", 0);

                }
            }   
        });

解决方案

I recently had the same need - to have a radio group where the selected item could be deselected by tapping it again. I found that I couldn't accomplish that using listeners but I was able to do it using a custom RadioButton, like so...

public class ToggleableRadioButton extends RadioButton {
    // Implement necessary constructors

    @Override
    public void toggle() {
        if(isChecked()) {
            if(getParent() instanceof RadioGroup) {
                ((RadioGroup)getParent()).clearCheck();
            }
        } else {
            setChecked(true);
        }
    }
}

Notice that the button is toggled in different ways depending on its current state - i.e., calling setChecked(true) on the button vs. calling clearCheck() on the group. If setChecked() is used in both cases, a button that was just deselected cannot be immediately re-selected - the logic in RadioGroup seems to immediately deselect it.

To use this button, just replace your <RadioButton> tags with <your.package.ToggleableRadioButton> in your layout XML.

这篇关于Android的单选按钮,不选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆