27

從我的理解,以確定是否一個複選框被「點擊」,如果它檢查或找不到,這樣的代碼可以使用下列內容:安卓:RadioGroup中 - 如何配置事件監聽器

cb=(CheckBox)findViewById(R.id.chkBox1); 
     cb.setOnCheckedChangeListener(this); 

public void onCheckedChanged(CompoundButton buttonView, 
    boolean isChecked) { 
     if (isChecked) { 
      cb.setText("This checkbox is: checked"); 
     } 
     else { 
      cb.setText("This checkbox is: unchecked"); 
     } 
    } 

但是,我無法制定出如何爲一個無線電組進行上述操作的邏輯。

這裏是我的RadioGroup中的xml:

<RadioGroup android:id="@+id/radioGroup1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
    <RadioButton android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/radio1" android:checked="true" 
    android:text="RadioButton1"> 
    </RadioButton> 
    <RadioButton android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/radio2" android:text="RadioButton2" android:checked="true"> 
    </RadioButton> 
    <RadioButton android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/radio3" android:text="RadioButton3"> 
    </RadioButton> 
</RadioGroup> 

問:我需要設置另一個監聽器,或將聽衆已經在那裏也「註冊」這個羣體?

另外,是否應該在RadioGroup或RadioButton上設置監聽器?

回答

74

這是你如何讓檢查單選按鈕:

// This will get the radiogroup 
RadioGroup rGroup = (RadioGroup)findViewById(r.id.radioGroup1); 
// This will get the radiobutton in the radiogroup that is checked 
RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(rGroup.getCheckedRadioButtonId()); 

要使用的監聽器,你這樣做:

+0

它說:「局部變量R-基可能尚未初始化」 – Ryan

+4

我只是寫了第一線,告訴你什麼是RGROUP。要獲得rGroup,你需要寫下:RadioGroup rGroup =(RadioGroup)findViewById(R.id.radioGroup1); –

+0

我不明白它,它告訴我該radiogroup被檢查...但不告訴我哪個單選按鈕被檢查。我錯過了什麼嗎? – Ryan

14

應該是這樣的。

RadioGroup rb = (RadioGroup) findViewById(R.id.radioGroup1); 
rb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      switch (checkedId) { 

      } 
     } 

    }); 

基礎上checkedId,你就會知道其中的單選按鈕被點擊,然後用你上面的代碼中找出如果選中或取消選中。這是作業。 ;)

+2

您的代碼工作,但我拿出了開關,並添加了這個:tv.setText(「blah:」+ checkedId) ;但它給了我一些奇怪的數字爲:( – Ryan

+0

)CheckedId是單選按鈕組中的單選按鈕的ID。您需要使用findViewbyId來理解它。 – PravinCG

+0

我希望這個工作,但它不。當我在3按鈕控件上更改選定的單選按鈕時,我得到3個已更改的通知,似乎每個按鈕都有一個按鈕,但對於第一個通知,checkedId按鈕的值爲Checked.Abiri的解決方案適用於我。 –

5
//Within the Activity that hosts this layout, the following method handles the click event for both radio buttons: 

public void onRadioButtonClicked(View view) { 
// Is the button now checked? 
boolean checked = ((RadioButton) view).isChecked(); 

// Check which radio button was clicked 
switch(view.getId()) { 
    case R.id.radio_pirates: 
     if (checked) 
      // Pirates are the best 
     break; 
    case R.id.radio_ninjas:      
     if (checked) 
      // Ninjas rule 
     break; 
} 
} 
0

使用開關更好:

radGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     public void onCheckedChanged(RadioGroup arg0, int id) { 
     switch (id) { 
     case -1: 
      Log.v(TAG, "Choices cleared!"); 
      break; 
     case R.id.chRBtn: 
      Log.v(TAG, "Chose Chicken"); 
      break; 
     case R.id.fishRBtn: 
      Log.v(TAG, "Chose Fish"); 
      break; 
     case R.id.stkRBtn: 
      Log.v(TAG, "Chose Steak"); 
      break; 
     default: 
      Log.v(TAG, "Huh?"); 
      break; 
     } 
     } 
    });