2014-02-19 25 views
0


我有一組RadioGroup有七個元素,其中每個RadioButton都未選中;我需要知道什麼時候第一次檢查RadioButton(每個更改都沒有考慮)。我怎樣才能做到這一點?如何知道一個RadioGroup RadioButton的全部選中狀態時的第一次檢查

這裏是我的嘗試:

ratingRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){ 
      public void onCheckedChanged(RadioGroup rGroup, int checkedId){ 
       if(oneRadioButton.isChecked() == false && twoRadioButton.isChecked() == false && 
         threeRadioButton.isChecked() == false && fourRadioButton.isChecked() == false && 
         fiveRadioButton.isChecked() == false && sixRadioButton.isChecked() == false && 
         sevenRadioButton.isChecked() == false && taskEditTextShown == true){ 
        lastTime = System.currentTimeMillis(); 
       } 
      } 
     }); 

但這代碼不起作用。

+0

任何'RadioButton'或一個特定的? –

+0

任何'RadioButton' – DamianFox

+0

你檢查組中是否所有的raido按鈕都是錯誤的?我認爲這種情況不會發生,這種情況在你第一次開始活動時只有 –

回答

0

對於初學者來說,它看起來像缺少了@Override Annotation。您必須確保添加它,因爲該方法正被其超類重寫。

它更改爲以下:

ratingRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){ 
     // Missing this Line below 
     @Override 
     public void onCheckedChanged(RadioGroup rGroup, int checkedId) 
     {    
      // This Code is not a good Way to do this... 
      if((oneRadioButton.isChecked() == false) && 
       (twoRadioButton.isChecked() == false) && 
       (threeRadioButton.isChecked() == false) && 
       (fourRadioButton.isChecked() == false) && 
       (fiveRadioButton.isChecked() == false) && 
       (sixRadioButton.isChecked() == false) && 
       (sevenRadioButton.isChecked() == false) && 
       (taskEditTextShown == true)) 
      { 
       lastTime = System.currentTimeMillis(); 
      } 
     } 
    }); 

這裏是一個更好的辦法

相反,我會用這樣的事情從我剛剛創建,其使用switch語句,以保持應用程序採取跟蹤RadioGroup上的點擊。

因此,這裏是充滿我的MainActivity的以跟蹤單選按鈕組的點擊,第一次,如果被點擊一次按鈕其從組重置爲0

public class MainActivity extends Activity { 

// Create Some Class Members for handling our actions an UI Elements 
private static final String TAG = "MainActivity"; 

private RadioGroup m_radioFruits  = null; 
private RadioButton m_radioApples  = null, 
        m_radioBananas  = null, 
        m_radioCantaloupe = null, 
        m_radioPears  = null, 
        m_radioWatermelon = null; 

private int m_counterApples = 0, 
      m_counterWatermelon = 0, 
      m_counterBananas = 0, 
      m_counterCantaloupe = 0, 
      m_counterPears = 0; 




@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Reference the radiogroup and add a OnCheckedChangeListener 
    m_radioFruits = (RadioGroup) findViewById(R.id.radioFruits); 
    m_radioFruits.setOnCheckedChangeListener(RadioFruitListener); 

    // Reference all the UI Elements inside the radioGroup (i.e. all the RadioButtons) 
    m_radioApples  = (RadioButton) findViewById(R.id.radioApples); 
    m_radioBananas  = (RadioButton) findViewById(R.id.radioBananas); 
    m_radioCantaloupe = (RadioButton) findViewById(R.id.radioCantaloupe); 
    m_radioPears  = (RadioButton) findViewById(R.id.radioPears); 
    m_radioWatermelon = (RadioButton) findViewById(R.id.radioWatermelon); 

} 

    // Not Necessary - Generated By Eclipse 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

// -------------------------------------------------------------------------- 
// Listener 
OnCheckedChangeListener RadioFruitListener = new OnCheckedChangeListener() 
{ 
     // Missing this before 
    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) 
      { 
        // You can get the Id of the RadioButton this way 
     int radioButtonId = group.getCheckedRadioButtonId(); 

        // or Something Like 
        // int radioButtonId = checkedId; 

        //^--- both give you the same reference 

        // Log this reference for debugging 
     Log.d(TAG, "radioButtonID = "+ Integer.toString(radioButtonId)); 

        // --------------- ALL THE ACTION IS HERE ------------------- 

     switch(radioButtonId) 
     { 
        // For each button repeat the steps 1-3 
     case R.id.radioApples: 
      // 1. Check the Radio Button 
          m_radioApples.setChecked(true); 

      // 2. Increment Counter 
      m_counterApples += 1; 

          // 3. See if it has been clicked 
      if(m_counterApples > 1) 
      { 
            // if so initialize it to 0 again 
       m_counterApples = 0; 
      } 
      break; 
     case R.id.radioBananas: 
      m_radioBananas.setChecked(true); 

      m_counterBananas += 1; 

      if(m_counterBananas > 1) 
      { 
       m_counterBananas = 0; 
      } 
      break; 
     case R.id.radioCantaloupe: 
      m_radioCantaloupe.setChecked(true); 

      m_counterCantaloupe += 1; 

      if(m_counterCantaloupe > 1) 
      { 
       m_counterCantaloupe = 0; 
      } 

      break; 
     case R.id.radioPears: 
      m_radioPears.setChecked(true); 

      m_counterPears += 1; 

      if(m_counterPears > 1) 
      { 
       m_counterPears = 0; 
      } 
      break; 
     case R.id.radioWatermelon: 
      m_radioWatermelon.setChecked(true); 

      m_counterWatermelon += 1; 

      if(m_counterWatermelon > 1) 
      { 
       m_counterWatermelon = 0; 
      } 
      break; 
     } 

     // Prints out the count of each Item everytime a radiobutton is selected 
     Log.d(TAG, "CounterApples = "+ Integer.toString(m_counterApples)); 
     Log.d(TAG, "CounterBananas = "+ Integer.toString(m_counterBananas)); 
     Log.d(TAG, "CounterCantaloupe = "+ Integer.toString(m_counterCantaloupe)); 
     Log.d(TAG, "CounterPears = "+ Integer.toString(m_counterPears)); 
     Log.d(TAG, "CounterWatermelon = "+ Integer.toString(m_counterWatermelon)); 


    } 

}; 
} 
相關問題