【问题标题】:How do I deselect/uncheck all checkbox buttons when one is selected?选中一个复选框后,如何取消选择/取消选中所有复选框按钮?
【发布时间】:2014-05-23 02:44:05
【问题描述】:

我在我的 java 代码中为 xml 中的按钮设置了一个切换开关。代码如下:

@Override
    public void onClick(final View view) {
        switch (view.getId()) {
        case R.id.selectable_text:
            if(view instanceof CheckedTextView){
                CategoryCheckableRow checkableRow = ((CheckedTextView)view).getCategoryCheckableRow();
                toggleCategoryRow(view, checkableRow);
                Log.d("newsdash","I am toggling in dash onclick");
                if (!mCategoriesSet.add(checkableRow)) {
                    mCategoriesSet.remove(checkableRow);
                }
                mDoneButton.setEnabled(!mCategoriesSet.isEmpty());
            }
            return;
        case R.id.button_done:
            sendCategoriesToActivity();
            ((DashboardManageNewsCategoriesActivity) getActivity()).updateCategoriesMap(mCategoriesSet);
            break;
        default:
        }

}

当我的当前复选框被选中时,我计划取消选中所有其他“检查”。假设我有复选框 1、2 和 3,如果我选中 1,我希望取消选中 2 和 3,依此类推。有没有办法用上面的代码实现这一点?说(如果光标(或可能视图)。getcursor 或当前位置)=当前复选框已选中){取消选中除当前复选框之外的所有其他复选框}?

这里也是切换行:

private void toggleCategoryRow(final View view, final CategoryCheckableRow checkableRow) {
        final CheckedTextView textView = (CheckedTextView) view;
        textView.toggle();
        checkableRow.setSelected(textView.isChecked());
    }

这里是对应的xml文件(供参考):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/altercolor2"
    android:orientation="vertical">
  <ScrollView 
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/altercolor2"
        android:orientation="vertical" >



        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <Button
                android:id="@+id/button_top_news"
                android:layout_width="match_parent"
                android:layout_height="@dimen/manage_news_btn_ht"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="2dp"
                android:background="@drawable/manage_market_category_btnbg"
                android:gravity="left|center_vertical"
                android:paddingLeft="10dp"
                android:drawablePadding="10dp"
                android:text="@string/button_top_news"
                android:textColor="#cccccc"
                android:textSize="@dimen/title" 
                android:drawableLeft="@drawable/arrow_expand_collapse"/>




        </RelativeLayout>
</linearlayout>
</,... etc

【问题讨论】:

  • 您是否有理由不使用RadioGroup,因为这是它的预期功能,而不是Checkbox
  • 我不使用radiogroup的原因是我有不同的类别和自己的子类别,所以我发现很难维护,除了我以前从未使用过它们
  • 现在是开始的好时机。当用户看到复选框时,通常他们希望能够选中多个复选框。当他们看到一个广播组时,他们通常知道他们只能选择一个,选择一个会取消选中其他的。它确实会让您的生活更轻松,为您的用户带来更愉快的体验
  • 你能举一个关于我的代码的例子吗?我已经发布了我的 xml
  • @Emmanuel 那张照片比比伯还老哈哈,我20岁左右,现在27岁,他抄袭了我的风格哈哈

标签: android android-fragments android-button android-cursor android-checkbox


【解决方案1】:

我不确定你在哪里初始化CheckBoxes,所以我不确定你会把它放在哪里。但是当你创建它们时,只需将它们添加到 ArrayList 之类的

//initialize an ArrayList
ArrayList<CheckBox> checkArray = new ArrayList<CheckBox>();
// then add your checkboxes to the list
checkArray.add(aCheckBox);

然后在您的点击事件中

for(i=0; i<checkArray.size(); i++)
{
    if (checkArray.get(i) != (CheckBox)v)  // assuming v is the View param passed in
       checkArray.get(i).setChecked(false);
}

这可以写得更好,但我不确定你的代码的其余部分看起来如何,所以这应该会给你一个想法。

【讨论】:

  • 这是我的问题的一部分:stackoverflow.com/questions/22964603/… 如果有意义的话,它有一些详细的代码
  • 我不知道你会“getcurrentpositionof”什么?好的,我试试看
  • 谢谢!此外,我对使用它的基类进行了修改,并将代码添加到该问题以获取更多信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-27
  • 2018-04-14
  • 1970-01-01
  • 2011-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多