RadioGroup 和 checkedId
RadioGroup and checkedId
我上周刚开始学习,我对我书上的RadioGroup
有一些疑问。
radioGroup.clearCheck();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton) group.findViewById(checkedId);
switch (rb.getId()) {
case R.id.radioButtonLondon:
tClock.setTimeZone("Europe/London");
break;
case R.id.radioButtonBeijing:
tClock.setTimeZone("CST6CDT");
break;
case R.id.radioButtonNewYork:
tClock.setTimeZone("America/New_York");
break;
}
// End switch block
//}
}
});
- 我的书上写着
RadioButton rb = (RadioButton) group.findViewById(checkedId);
习惯于
"get a reference to the actual object that checkedId is referring to,
then we can retrieve the familiar ID that we use for the currently
selected radio button, for which we now have a reference stored in
rb."
我对这个解释很困惑
- 这条线
RadioButton rb = (RadioButton) group.findViewById(checkedId);
是必须的吗?我试图隐藏这一行并将 switch (rb.getId())
更改为 switch(checkedId)
并且一切仍然正常。
谢谢!
不要定义 radiobutton.In setOnCheckedChangeListener 本身,您将获得单选按钮 id.Remove RadioButton rb = (RadioButton) group.findViewById(checkedId);
您的代码应如下所示:
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radioButtonLondon:
tClock.setTimeZone("Europe/London");
break;
case R.id.radioButtonBeijing:
tClock.setTimeZone("CST6CDT");
break;
case R.id.radioButtonNewYork:
tClock.setTimeZone("America/New_York");
break;
}
// End switch block
//}
}
});
看到您只是在更新 "tClock" 那么您认为该行没有必要是对的。如果出于某些其他原因,您需要为单选按钮调用任何可用方法,那将是必要的。
我上周刚开始学习,我对我书上的RadioGroup
有一些疑问。
radioGroup.clearCheck();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton) group.findViewById(checkedId);
switch (rb.getId()) {
case R.id.radioButtonLondon:
tClock.setTimeZone("Europe/London");
break;
case R.id.radioButtonBeijing:
tClock.setTimeZone("CST6CDT");
break;
case R.id.radioButtonNewYork:
tClock.setTimeZone("America/New_York");
break;
}
// End switch block
//}
}
});
- 我的书上写着
RadioButton rb = (RadioButton) group.findViewById(checkedId);
习惯于
"get a reference to the actual object that checkedId is referring to, then we can retrieve the familiar ID that we use for the currently selected radio button, for which we now have a reference stored in rb."
我对这个解释很困惑
- 这条线
RadioButton rb = (RadioButton) group.findViewById(checkedId);
是必须的吗?我试图隐藏这一行并将switch (rb.getId())
更改为switch(checkedId)
并且一切仍然正常。
谢谢!
不要定义 radiobutton.In setOnCheckedChangeListener 本身,您将获得单选按钮 id.Remove RadioButton rb = (RadioButton) group.findViewById(checkedId);
您的代码应如下所示:
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radioButtonLondon:
tClock.setTimeZone("Europe/London");
break;
case R.id.radioButtonBeijing:
tClock.setTimeZone("CST6CDT");
break;
case R.id.radioButtonNewYork:
tClock.setTimeZone("America/New_York");
break;
}
// End switch block
//}
}
});
看到您只是在更新 "tClock" 那么您认为该行没有必要是对的。如果出于某些其他原因,您需要为单选按钮调用任何可用方法,那将是必要的。