我有一组RadioGroup
,其中有七个元素,其中每个RadioButton
都未选中;我需要知道第一次检查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 :(得分: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));
}
};
}