简体   繁体   English

Android 数据绑定,点击按钮提交时 RadioGroup 的 Select 值

[英]Android data binding, Select value of RadioGroup when click button submit

I have a RadioGroup consisting of 2 choices and a button, After I press that button I want to get the value and based on that value I want to perform different actions:我有一个 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()
            }
        }

    }

But my above code is not suitable for databinding so can anyone help me to solve this problem?但是我上面的代码不适合数据绑定,所以任何人都可以帮我解决这个问题吗?

Can't see any obstacle for it.看不到任何障碍。 Just handle two actions separately, when radio button is checked "android:checked="@{viewModel.selectedRG.equals(RG.A)}", when button pressed onSingleClick="@{viewModel.performSomeAction()}". So in this case when performSomeAction() will be invoked RG type can be taken from "selectedRG". If smth missed, let me know. Just handle two actions separately, when radio button is checked "android:checked="@{viewModel.selectedRG.equals(RG.A)}", when button pressed onSingleClick="@{viewModel.performSomeAction()}". So in这种情况下 performSomeAction() 将被调用 RG 类型可以从“selectedRG”中获取。如果 smth 错过了,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM