代码之家  ›  专栏  ›  技术社区  ›  Vishal Vaishnav

取消选中RecyclerView中的选中单选按钮

  •  0
  • Vishal Vaishnav  · 技术社区  · 6 年前

    我想取消选中选中单选按钮的第二次点击(这是一个单一的选择单选按钮代码)

    下面是我的代码:

    int row_index = -1;
    
    holder.rbBookOfferText.setOnClickListener(view -> {
            holder.rbBookOfferText.setChecked(true);
    
            row_index = Integer.parseInt(mDataset.get(position).getTaskId());
            notifyDataSetChanged();
    
        });
    
        if (row_index == Integer.parseInt(mDataset.get(position).getTaskId())) {
            holder.rbBookOfferText.setChecked(true);
        } else {
            holder.rbBookOfferText.setChecked(false);
        }
    

    enter image description here

    3 回复  |  直到 6 年前
        1
  •  3
  •   Amit Viradiya    6 年前

    你必须把 holder.rbBookOfferText.setChecked(true); 在第一次单击时以编程方式进行。所以 rbBookOfferText公司

        2
  •  0
  •   Vishal Vaishnav    6 年前

    我得到了答案;

        holder.tvBookOfferText.setOnClickListener(view -> {
    
                if (mDataset.get(position).getTaskId().equalsIgnoreCase(String.valueOf(row_index))) {
                    holder.tvBookOfferText.setChecked(false);
                    row_index = -1;
                } else {
                    holder.tvBookOfferText.setChecked(true);
                    row_index = Integer.parseInt(mDataset.get(position).getTaskId());
                    notifyDataSetChanged();
                }
    
            });
    
            if (row_index == Integer.parseInt(mDataset.get(position).getTaskId())) {
                holder.tvBookOfferText.setChecked(true);
            } else {
                holder.tvBookOfferText.setChecked(false);
            }
    
        3
  •  -1
  •   Android Geek    6 年前

    若要取消选中单选按钮,请再次单击您需要使用的单选按钮 一个单选组下的所有单选按钮 .

        <?xml version="1.0" encoding="utf-8"?>
        <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <RadioButton android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Place 3 order of Margherita and on 4th order get $20 off"
                android:onClick="onRadioButtonClicked"/>
           <RadioButton android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hourly based order"
                android:onClick="onRadioButtonClicked"/>
          <RadioButton android:id="@+id/radio3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Place 3 order of Pugliese and get 4th Pugliese free"
                android:onClick="onRadioButtonClicked"/>
          <RadioButton android:id="@+id/radio4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="40% order off on Place order "
                android:onClick="onRadioButtonClicked"/>
        </RadioGroup>