【问题标题】:Android data binding, Select value of RadioGroup when click button submitAndroid数据绑定,点击按钮提交时选择RadioGroup的值
【发布时间】:2023-04-03 22:49:01
【问题描述】:

我有一个 RadioGroup,由 2 个选项和一个按钮组成,按下该按钮后,我想获取值并根据该值执行不同的操作:


 <RadioGroup
            android:id="@+id/rg_select"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@+id/tv_title">

            <RadioButton
                android:id="@+id/rb_a"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableStart="@drawable/ic_mo_512dp"
                android:drawablePadding="20dp"
                android:paddingStart="10dp"
                android:paddingEnd="0dp"
                android:text="Visual Advance" />

            <RadioButton
                android:id="@+id/rb_b"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawablePadding="20dp"
                android:text="Visual basic" />
        </RadioGroup>

   <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click"
            onSingleClick="@{() -> onClickListener.getRadioButton()}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/rg_select">
   fun getRadioButton() {
        val result = rg_select.checkedRadioButtonId
        when (genderId) {
            R.id.rb_a -> {
                Toast.makeText(context, "KKSKSK", Toast.LENGTH_LONG).show()
            }
            R.id.rb_b -> {
                Toast.makeText(context, "KKSKSK", Toast.LENGTH_LONG).show()
            }
        }

    }

但是我上面的代码不适合数据绑定,有谁能帮我解决这个问题吗?

【问题讨论】:

    标签: java android android-studio kotlin data-binding


    【解决方案1】:

    看不到任何障碍。只需处理两个操作,当检查单选按钮“android:checked =”@ {ViewModel.SelectedRG.Equals(RG.A)}“,当按钮按下OnsingleClick =”@ {ViewModel.performsomeAction()}“。所以这种情况下,将调用 performSomeAction() 可以从“selectedRG”中获取 RG 类型。 如果错过了,请告诉我。

    【讨论】:

      猜你喜欢
      • 2014-07-29
      • 2016-12-07
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多