gpt4 book ai didi

java - RadioGroup 和checkedId

转载 作者:行者123 更新时间:2023-12-01 09:04:05 28 4
gpt4 key购买 nike

我上周刚刚开始学习,我对我书上的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

//}
}
});
<小时/>
  1. 在我的书中,它说 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) 并且一切仍然正常。
  • 谢谢!

    最佳答案

    不要定义单选按钮。在 setOnCheckedChangeListener 本身中,您将获得单选按钮 id。删除 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

    //}
    }
    });

    关于java - RadioGroup 和checkedId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41428456/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com