2013-04-05 128 views
2

该应用程序是一个步进音序器应用程序,每个组中有8个按钮,共有16个电台组。除非我使用我创建的清除按钮来清除所有的radiogroups,否则一旦某个组选择了按钮,我无法关闭。我想添加的是一些代码,说当一个选定的单选按钮被再次选中时,它就会像切换一样关闭。我尝试过使用切换,但随后出现了其他问题。下面是两次尝试但都只是使用按钮Android单选按钮取消选中

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

       } 
      } 
     }); 
+0

这通常不是如何'RadioButtons'工作,而不是人们期望的。您应该为您的群组添加一个'无'的'RadioButton' – codeMagic 2013-04-05 14:45:45

+0

为什么不给每个组添加一个OFF按钮? – 2013-04-05 14:43:14

+0

我可以做到这一点,但因为它是单声道的,我只想在每个组中选择一个 – 2013-04-05 14:47:46

回答

14

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

public class ToggleableRadioButton extends RadioButton { 
    // Implement necessary constructors 

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

注意,按钮被以不同的方式切换取决于其当前状态 - 即在该按钮上调用setChecked(true)而在该组上调用clearCheck()。如果在两种情况下都使用setChecked(),那么刚刚取消选择的按钮不能立即重新选择 - RadioGroup中的逻辑似乎立即取消选择它。

要使用此按钮,只需将您的<RadioButton>标签替换为您的布局XML中的<your.package.ToggleableRadioButton>

3

它实际上可以与听众,但与OnTouchListener,将按钮的状态之前触发做了改变,而不是通常的OnClickListener。对我来说,以下工作:

View.OnTouchListener radioButtonOnTouchListener = new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     if (((RadioButton) v).isChecked()) { 
      // If the button was already checked, uncheck them all 
      radioGroup.clearCheck(); 
      // Prevent the system from re-checking it 
      return true; 
     } 
     return false; 
    } 
}; 
radioButton1.setOnTouchListener(radioButtonOnTouchListener); 
radioButton2.setOnTouchListener(radioButtonOnTouchListener); 

radioGroupradioButton1家长和radioButton2

+0

'onTouch()'是相当低级的。看起来这样会取消选择除点击/点击以外的手势按钮。 – spaaarky21 2016-09-01 22:28:57

+0

嗯,好点,@ spaaarky21。我想我们也应该检查事件的类型是“ACTION_DOWN”。虽然,测试这个,我还没有能够触发任何异常 – wamfous 2016-09-07 10:39:43

4

我只是用回答@spaaarky21

和我完整的代码看起来就像这样,它是工作的罚款!

的Java类

public class ToggleableRadioButton extends RadioButton { 


    public ToggleableRadioButton(Context context) { 
     super(context); 
    } 

    public ToggleableRadioButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public ToggleableRadioButton(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public ToggleableRadioButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

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

以及XML布局

<com.smart_dent.adapters.ToggleableRadioButton android:id="@+id/tejido_blando_perfil_convexo" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:text="@string/tejido_blando_convexo_label" /> 

在这种情况下,你只需要改变的包,我这是很容易找到,它只是在顶部Java类Flie(如果您是从Android Studio创建的)

1

编辑自@ spaaarky21回答

@Override 
public void toggle() { 
    if (isChecked()) { 
     if (getParent() instanceof RadioGroup) { 
      ((RadioGroup) getParent()).clearCheck(); 
     } 
     // add else here when a single radioButton without radioGroup 
     else { 
      setChecked(false); 
     } 
    } else { 
     setChecked(true); 
    } 
} 
0

这也做的工作:

public final class ToggleAbleRadioButton extends AppCompatRadioButton { 
    public ToggleAbleRadioButton(final Context context, final AttributeSet attrs) { 
    super(context, attrs); 
    } 

    @Override public void toggle() { 
    setChecked(!isChecked()); 
    } 
} 
+0

不起作用,如果你检查两次,它不会再检查,因为在appCompatRadioButoon thos代码 - 如果(mBroadcasting){0128}返回; } – blay 2017-06-21 18:16:06