【问题标题】:Disable button after all checkboxes are unchecked取消选中所有复选框后禁用按钮
【发布时间】:2018-04-14 18:01:19
【问题描述】:

我在recycler view adapter 中有multiple checkbox。在parent view 中有一个按钮。如果选中任何复选框,我想启用按钮,如果未选中所有复选框,我想启用disable 按钮。我该怎么做,

Holder.checboxone.setOnCheckedChangeListener(new 

 CompButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompButton compButton, 
    boolean b) {
                item.setChecked(b);

                checkboxStatus(b);

            }
        });


private void checkboxStatus(boolean isChecked) {

    if (isChecked) {
        ((MainActivity)context).getButton().setEnabled(true);

    } else  {
        ((MainActivity)context).getButton().setEnabled(false);
    }
}

根据我的代码,我可以在单击任何复选框时启用按钮,但禁用功能无法正常工作,如果我未选中单个复选框,按钮也会禁用,我想在所有复选框仅取消选中后禁用按钮

【问题讨论】:

  • 发布您的列表视图适配器类

标签: java android checkbox android-recyclerview


【解决方案1】:

在函数中检查是否使用循环检查了任何复选框。如果有人被选中,则设置一个标志。使用此标志来启用或禁用按钮。

参考资料:

【讨论】:

  • 这以 O(n) 复杂度运行,每次用户甚至触摸复选框。 ://
【解决方案2】:

内部活动(实现MyInterface

int selectedCount = 0; // Global variable

@Override
public void checkboxListener(boolean isSelected){
    if (isSelected)
        selectedCount++;
    else
        selectedCount--;
    if (selectedCount > 0)
        button.setEnabled(true);
    else
        button.setEnabled(false);
}

界面~~!!

public interface MyInterface {
    void checkboxListener(boolean isSelected);
}

从 Checkbox.onCheckedChangeListener() 内部调用此接口;在适配器内部。

更新

在您的适配器内部:

改变这个方法。

private void checkboxStatus(boolean isChecked) {
    myInterface.checkboxListener(isChecked);
}

您可能也在为您的适配器使用构造函数,将其更改为:

public void MyAdapter(Context c, Data d, MyInterface myInterface){
    ...
    this.myInterface = myInterface;
}

内部活动:

public class MyActivity extends Activity implements MyInterface { ...

当你初始化你的适配器时,这样做:

MyAdapter myAdapter = new MyAdapter(this /* context */, data /* whatever is your data */, this);

在您的活动中添加第一块(我的答案)中的代码,您就可以开始了!!!

【讨论】:

  • 谢谢你..你能编辑我的代码吗。我没有得到你的界面部分。
【解决方案3】:

我希望我不会太晚。

var checker = document.querySelectorAll("#checkme");
var connect = document.getElementById("connect");

checker.forEach(function (element) {
  element.onchange = function () {
    connect.disabled = true;
    checked = document.querySelectorAll("#checkme:checked");
    if (checked.length == checker.length) {
      connect.disabled = false;
    }
  };
});
<script src="https://getuikit.com/assets/uikit/dist/js/uikit-icons.js"></script>
<script src="https://getuikit.com/assets/uikit/dist/js/uikit.js"></script>
<link href="https://getuikit.com/assets/uikit/dist/css/uikit.css" rel="stylesheet"/>
<div class="uk-container uk-margin-large-top">
  <form>
    <fieldset class="uk-fieldset">

      <legend class="uk-legend">All Boxes must be checked</legend>
      <p>Button will be enabled once all boxes checked</p>

      <div class="uk-margin uk-grid-small uk-child-width-1-1 uk-grid">

        <label><input id="checkme" class="uk-checkbox" type="checkbox"> A</label>
        <label><input id="checkme" class="uk-checkbox" type="checkbox"> B</label>
        <label><input id="checkme" class="uk-checkbox" type="checkbox"> B</label>
        <label><input id="checkme" class="uk-checkbox" type="checkbox"> C</label>
        <div class="uk-margin-small-top">
          <button id="connect" class="lab-search-btn uk-button uk-button-secondary lab-darkblue" uk-toggle="target: #phoneconnect" disabled><i uk-icon="check"></i> Agree to A, B, C, D </button>
        </div>

    </fieldset>
  </form>
  </div>

请找到我使用工作示例创建的代码片段和代码笔的链接: https://codepen.io/team/lfprofessors/pen/dyVjXBv

【讨论】:

    猜你喜欢
    • 2014-05-23
    • 1970-01-01
    • 2021-12-06
    • 2013-06-08
    • 1970-01-01
    • 2019-02-14
    • 2011-07-24
    • 1970-01-01
    • 2021-12-18
    相关资源
    最近更新 更多