如果选择一个复选框按钮,如何取消选中/取消选中所有复选框按钮?

时间:2014-04-09 17:53:54

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

我的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或currentposition)=当前复选框已选中){取消选中除当前复选框以外的所有其他复选框}?

这里也是切换行:

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

这里; s相应的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

1 个答案:

答案 0 :(得分:0)

我不确定你在初始化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);
}

这可以写得更好,但我不确定你的代码的其余部分是什么,所以这应该给你一个想法。

相关问题