上週我剛開始學習,我對我的書上的RadioGroup
有一些疑問。RadioGroup and checkedId
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);
用於
「得到一個參考實際的對象checkedId指的是, 那麼我們就可以檢索我們用於當前 所選單選按鈕的熟悉ID,爲此我們現在使用存儲在 rb中的參考文獻「。
我很困惑這個交代
- 這是行
RadioButton rb = (RadioButton) group.findViewById(checkedId);
有必要嗎?我試圖隱藏這條線,並將switch (rb.getId())
更改爲switch(checkedId)
,一切仍然正常。
謝謝!
你不需要這條線..RadioButton rb =(RadioButton)group.findViewById(checkedId); – rafsanahmad007
看到這個https://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html – rafsanahmad007