【问题标题】:Uncheck checked radio button in RecyclerView取消选中 RecyclerView 中的选中单选按钮
【发布时间】:2018-10-26 10:52:18
【问题描述】:

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

下面是我的代码:

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);
    }

【问题讨论】:

  • 使用setOnCheckedChangeListener 而不是setOnClickListener
  • 我想在第二次点击同一个单选按钮时取消选中单选按钮
  • @Piyush 你能给我举个例子吗?
  • @VishalVaishnav 那么是否有所有 4 个单选按钮都被取消选中的情况?!!!
  • @VishalVaishnav 这是您使用复选框而不是单选按钮的情况。

标签: android android-recyclerview


【解决方案1】:

您必须在第一次点击时以编程方式设置 holder.rbBookOfferText.setChecked(true);。因此,rbBookOfferText 不会在第一次点击时取消选中。

【讨论】:

    【解决方案2】:

    我得到了答案;

        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】:

      要在第二次单击同一个单选按钮时取消选中单选按钮,您需要选择一个单选组下的所有单选按钮

          <?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>
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 2012-03-17
      • 2020-04-29
      • 2016-01-23
      • 2012-01-31
      相关资源
      最近更新 更多