【问题标题】:Recyclerview radio button group get the checked radioRecyclerview 单选按钮组获取选中的单选
【发布时间】:2019-09-27 16:34:58
【问题描述】:

我的 recylerview 行项目中有以下内容

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:id="@+id/radioItems"
            android:layout_height="wrap_content"
            android:weightSum="2"
            android:orientation="horizontal">
            <RadioButton android:id="@+id/radio_yes"
                android:layout_width="wrap_content"
                android:layout_weight="0.8"
                android:layout_height="wrap_content"
                android:textSize="12sp"
                android:text="Yes"/>
            <RadioButton android:id="@+id/radio_no"
                android:layout_width="wrap_content"
                android:layout_weight="1"
                android:textSize="12sp"
                android:layout_height="wrap_content"
                android:text="No"/>
        </RadioGroup>

所以我想在我的 recyclerview 适配器中监听 radiogroup 项目何时被检查并获取它是否已被检查。所以在我的 bindviewholder 上的 recyclerview 中我有

      holder.radioItems.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            Log.i("test",""+group.getCheckedRadioButtonId());
        }
    });

但是当我检查我的日志时,我可以看到像 2131230980, 2131230979 这样的值,但看不到单选按钮的 id

我希望在日志中看到 radio_yesradio_no 的 id。 我错过了什么。 我仍然想使用 radiogroup 而不是独立的单选按钮。

【问题讨论】:

  • 21312309802131230979 是单选按钮的 ID。
  • 我想获取单选按钮上设置的 id 名称,分别是 radio_yes 和 no

标签: java android android-recyclerview android-radiogroup


【解决方案1】:

你可以看到这个答案 https://stackoverflow.com/a/29818783/5523669

getResources().getResourceEntryName(getId())

【讨论】:

    【解决方案2】:

    您可以简单地使用 switch case 在 setOnCheckedChangeListener

    中找到当前选中的 Id
           switch (checkedId) {
                case R.id.radio_yes :
                  Log.i("test","radio_no");
                 break;
                 case R.id.radio_no:
                  Log.i("test","radio_yes");
                 break;
    
            }
    

    或者比switch case更简单,你可以使用下面的代码

    String currentCheckedId=getResources().getResourceEntryName(checkedId);
    Log.d("test",currentCheckedId);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-29
      • 2019-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多