如何在带有数据绑定的checkChanged事件上绑定RadioGroup上的方法 [英] How to bind method on RadioGroup on checkChanged event with data-binding

查看:165
本文介绍了如何在带有数据绑定的checkChanged事件上绑定RadioGroup上的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在互联网上搜索如何在 RadioGroup 上执行新的酷炫的android数据绑定,但没有找到任何有关它的博客文章。

I was searching over the internet for how to perform the new cool android data-binding over the RadioGroup and I didn't find a single blog post about it.

这是一个简单的场景,基于选中的单选按钮,我想使用android数据绑定附加回调事件。我在xml部分上找不到任何允许我定义回调的方法。

Its a simple scenario, based on the radio button selected, I want to attach a callback event using android data binding. I don't find any method on the xml part which allows me to define a callback.

就像这里是我的RadioGroup:

Like here is my RadioGroup:

      <RadioGroup
            android:id="@+id/split_type_radio"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:checkedButton="@+id/split_type_equal"
            android:gravity="center"
            <!-- which tag ? -->
            android:orientation="horizontal">

           ...

       </RadioGroup>

我如何附加将在 RadioGroup上调用的处理程序方法 checkChnged 事件将使用数据绑定触发吗?

How do I attach a handler method which will be called on RadioGroup's checkChnged event will fire using data-binding?

我尝试使用 onClick (不知道是否相同),并在Activity中定义方法,并使用它在布局文件中定位它:

I have tried using onClick (don't know if it is the same) in layout file and defining method in the Activity and located it using this in the layout file:

   <variable
        name="handler"
        type="com.example.MainActivity"/>

  ...
   <RadioGroup
        android:onClick="handler.onCustomCheckChanged"
        .. >

然后定义方法 onCustomCheckChanged ,如下所示:

And defined method onCustomCheckChanged like this:

public void onCustomCheckChanged(RadioGroup radio, int id) {
     // ...
}

但是,它给了我编译错误:

But, it gives me the compilation error:


错误:(58,36)具有方法onClick的侦听器类android.view.View.OnClickListener与任何方法处理程序的签名都不匹配。onCustomCheckChanged

Error:(58, 36) Listener class android.view.View.OnClickListener with method onClick did not match signature of any method handler.onCustomCheckChanged

我见过很多博客都提到 RadioGroup 是有可能的,但是没有一个人真的说过。如何使用数据绑定处理此问题?

I have seen many blogs mentioning it is possible with RadioGroup but non of them really say how. How can I handle this with data-binding ?

推荐答案

在研究了一堆方法之后,我发现了关于SO的这个问题,它帮助我了解了如何绑定侦听器的单个方法。

After digging to the bunch of methods, I found this question on SO which helped me understand how to bind single methods of listeners.

这是RadioGroup的处理方法:

Here is what to do with RadioGroup:

RadioGroup 侦听器中,您有一个方法 onCheckedChanged(RadioGroup g,int id)。因此,您可以将该方法的实例作为变量传递到布局文件中,然后调用具有相同签名的方法,从而将该方法直接绑定到处理程序或活动。

In RadioGroup listener you have a method onCheckedChanged(RadioGroup g, int id). So you can directly bound that method to your handler or your activity by passing an instance of it as a variable in layout file and calling a method with the same signature.

So像这样调用布局文件:

So call on layout file like this:

  <RadioGroup
        android:id="@+id/split_type_radio"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:checkedButton="@+id/split_type_equal"
        android:gravity="center"
        android:onCheckedChanged="@{handler.onSplitTypeChanged}"
        android:orientation="horizontal">

       ...

   </RadioGroup>

在我的活动或处理程序中,我只需要简单地提供具有相同名称和签名的方法,就像这样:

And in my activity or handler, I need to simply provide the method with same name and signature like this:

public void onSplitTypeChanged(RadioGroup radioGroup,int id) {
  // ...
}

只需确保方法是公共的。

Just make sure method is public.

注意::此方法适用于任何(大多数情况下,我还没有尝试全部)侦听器方法。像 EditText 一样,您可以提供 android:onTextChanged 等。

NOTE: This works for any (most of, I have not tried all) listener methods. Like for EditText you can provide android:onTextChanged and so on.

这篇关于如何在带有数据绑定的checkChanged事件上绑定RadioGroup上的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆