Java 当放射组中的所有RadioButton均未选中时,如何知道对RadioButton的第一次检查

Java 当放射组中的所有RadioButton均未选中时,如何知道对RadioButton的第一次检查,java,android,Java,Android,我有一组RadioGroup,共有七个元素,其中每个RadioButton都未选中;我需要知道何时第一次检查单选按钮(不考虑以下每次更改)。我该怎么做 以下是我的尝试: ratingRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){ public void onCheckedChanged(RadioGroup rGroup, int checkedId){


我有一组
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();
                }
            }
        });

但是这段代码不起作用。

对于初学者来说,它似乎缺少了
@Override注释。您必须确保添加此项,因为该方法正在从其超类重写

将其更改为以下内容:

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上的单击

下面是我的主要活动的全部内容,它记录了第一次单击单选组按钮的情况,如果再次单击按钮,它将重置为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));


    }

};
}

组中的任何
单选按钮或特定按钮?检查组中的所有raido按钮是否为假??我认为这种情况不会发生,这种情况发生在你第一次开始活动时,你说的“这个代码不起作用”是什么意思?你有什么错误吗?@PM77-1代码没有给我任何错误,但结果不是我想要的expect@mohammedmomn我检查是否所有单选按钮都未选中,因为我需要知道选择的第一个单选按钮。。