2013-04-05 127 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