【问题标题】:Radiogroup with Togglebuttons not working properly带有切换按钮的 Radiogroup 无法正常工作
【发布时间】:2012-11-27 23:18:25
【问题描述】:

我尝试实现一个带有四个切换按钮的无线电组。所需的行为是典型的单选组行为:一个按钮被预选,如果用户单击另一个按钮,则取消选择前一个按钮,而选择新的按钮。如果用户再次单击选定的按钮,则不会发生任何事情,因为不允许没有选择任何按钮。这就是我遇到问题的地方。我按照这个问题的解决方案:Android: How to get a radiogroup with togglebuttons?

不幸的是,用户仍然可以取消选择活动按钮。我怎样才能防止这种情况发生?

这是我的代码:

ToggleButtons 的 onClick 监听器:

/**
 * Handler for onClick Events.
 */
    @Override
public void onClick(View v) {
    if (viewListener == null) {
        return;
    }
    if (v == tb_one|| v == tb_two|| v == tb_three|| v == tb_four) {
        ((RadioGroup) v.getParent()).check(v.getId());
    }
    else {
        super.onClick(v);
    }

}

我的自定义 OnCheckedChangeListener:

/**
 * The listener for a checked change event of the toggle buttons.
 */
static final RadioGroup.OnCheckedChangeListener ToggleListener = new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(final RadioGroup radioGroup, final int i) {
        //if one button is checked, uncheck all others
        for (int j = 0; j < radioGroup.getChildCount(); j++) {
            final ToggleButton view = (ToggleButton) radioGroup.getChildAt(j);
            view.setChecked(view.getId() == i);
        }
    }
};

这里是我添加监听器的地方:(它在 onFinishInflate 方法中)

    ((RadioGroup) findViewById(R.id.instant_toggleGroup_mode))
            .setOnCheckedChangeListener(ToggleListener);
    tb_one = (ToggleButton) findViewById(R.id.instant_tb_one);
    tb_one.setOnClickListener(this);
    tb_two = (ToggleButton) findViewById(R.id.instant_tb_two);
    tb_two.setOnClickListener(this);
    tb_three = (ToggleButton) findViewById(R.id.instant_tb_three);
    tb_three.setOnClickListener(this);
    tb_four = (ToggleButton) findViewById(R.id.instant_tb_four);
    tb_four.setOnClickListener(this);

如果有人可以向我指出解决方案,那就太好了!

【问题讨论】:

    标签: android radio-button listener togglebutton oncheckedchanged


    【解决方案1】:

    您检查 ToggleButton 是否已被选中。如果是,你什么都不做。

    if (v == tb_one|| v == tb_two|| v == tb_three|| v == tb_four) {
        if(!((ToggleButton) v).isChecked())
            ((RadioGroup) v.getParent()).check(v.getId());
    }
    

    编辑

    if (v == tb_one|| v == tb_two|| v == tb_three|| v == tb_four) {
        ((RadioGroup) v.getParent()).check(v.getId());
    
        if(!((ToggleButton) v).isChecked())
            ((ToggleButton) v).setChecked(true);
    }
    

    【讨论】:

    • 感谢您的回答,但它不起作用。我仍然可以取消选择一个切换按钮,现在两个按钮也可以同时处于活动状态。
    • @friday 我现在用正确的代码更新了我的答案。不过,这个实现有点“hacky”。​​
    【解决方案2】:

    最后我想出了如何去做。感谢 Ole,您的帮助使我找到了这个解决方案。

    所以这是工作代码:

    初始化按钮和按钮组:

    tb_one = (ToggleButton) findViewById(R.id.instant_tb_one);
    tb_one.setOnClickListener(this);
    tb_two = (ToggleButton) findViewById(R.id.instant_tb_two);
    tb_two.setOnClickListener(this);
    tb_three = (ToggleButton) findViewById(R.id.instant_tb_three);
    tb_three.setOnClickListener(this);
    tb_four = (ToggleButton) findViewById(R.id.instant_tb_four);
    tb_four.setOnClickListener(this);
    rg_modes = (RadioGroup) findViewById(R.id.instant_toggleGroup_mode);
    rg_modes.setOnCheckedChangeListener(ToggleListener);
    rg_modes.clearCheck();
    rg_modes.check(tb_one.getId());
    

    onClick 处理程序:

    if (v == tb_one|| v == tb_two|| v == tb_three|| v == tb_four) {
      rg_modes.clearCheck();
      rg_modes.check(v.getId());
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-03
      • 2021-03-27
      • 2012-06-25
      • 2021-10-01
      • 2018-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多