2016-09-10 83 views
4

我在互联网上搜索如何执行新的很酷的android数据绑定在RadioGroup,我没有找到关于它的一篇博客文章。如何在数据绑定的checkChanged事件绑定RadioGroup方法

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

喜欢这里是我的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> 

如何附上处理方法将被称为上RadioGroupcheckChnged事件将触发使用数据绑定?

我一直在使用onClick(不知道这是否是相同的)布局文件,并确定在活动方法尝试和布局文件中使用这个位于它:

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

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

而定义的方法onCustomCheckChanged像这样:

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

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

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

我看到很多博客提到它可能与RadioGroup但他们没有真的说如何。我如何处理data-binding

+0

你看过[这个答案](http://stackoverflow.com/questions/38145783/cusom-event-attributes-in-android-bindings-apponmyevent/38197468#38197468)?我认为你可以做类似的事情。如果你仍然需要帮助。我会在周一看到它:) – yennsarah

+0

@Amylinn谢谢你的链接。我真的很感激它:)我可以尝试,但我应该在xml中做什么属性?就像单选按钮中的checkChanged事件一样,没有任何让我提供方法的属性。如果我实际更改单选按钮(例如,将按钮重置为默认值),则“onClick”可能不起作用。 – kirtan403

+1

我认为相应的属性应该是'app:onCheckedChangeListener'。 – yennsarah

回答

5

在挖掘了一堆方法之后,我发现this这个问题让我了解了如何绑定监听器的单一方法。

这里是做什么用的RadioGroup中:

RadioGroup监听你有一个方法onCheckedChanged(RadioGroup g, int id)。因此,您可以直接将该方法作为布局文件中的变量传递给您的处理程序或您的活动,并调用具有相同签名的方法。

在这样的布局文件,以便拨打:

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

在我的活动或处理程序中,我需要简单的提供方法,采用相同的名称和签名是这样的:

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

只是要肯定的方法是公开的。

注意:这适用于任何(大部分,我没有尝试过所有)侦听器方法。像EditText一样,您可以提供android:onTextChanged等。

+0

当我尝试在我的xml中使用android:onCheckedChanged它说未知属性,我设法获得数据绑定与点击侦听器一起工作,但这个相同的解决方案对我来说不是工作 –

+0

@JudeFernandes他们在目前的皮棉有一些错误在android工作室,但他们编译罚款和作品。只要忽略警告即可。你能解释一些关于你的问题吗? – kirtan403

+0

如何通过这种方式处理双向句柄? – droidev

2

我使用一个字符串,在这种情况下基于viewModel.getCommuteType() viewModel.setCommuteType(String)

<RadioGroup 
android:orientation="horizontal" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 

<RadioButton 
    android:checked="@{viewModel.commuteType.equals(Commute.DRIVING)}" 
    android:onClick="@{()->viewModel.setCommuteType(Commute.DRIVING)}" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="D"/> 

<RadioButton 
    android:checked="@{viewModel.commuteType.equals(Commute.BICYCLE)}" 
    android:onClick="@{()->viewModel.setCommuteType(Commute.BICYCLE)}" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="B"/> 

<RadioButton 
    android:checked="@{viewModel.commuteType.equals(Commute.WALKING)}" 
    android:onClick="@{()->viewModel.setCommuteType(Commute.WALKING)}" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="W"/> 

<RadioButton 
    android:checked="@{viewModel.commuteType.equals(Commute.BUS)}" 
    android:onClick="@{()->viewModel.setCommuteType(Commute.BUS)}" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="T"/> 

0

我有绑定通常你关心什么实际检查,而不是“有什么东西在检查”更多。在这种情况下的替代解决方案是忽略RadioGroup中并且如下面结合所有项目:

<RadioGroup (...) > 
     <RadioButton (...) 
      android:checked="@={viewModel.optionA}"/> 

     <RadioButton (...) 
      android:checked="@={viewModel.optionB}"/> 

     <RadioButton (...) 
      android:checked="@={viewModel.optionC}"/> 
</RadioGroup> 

其中optionA,optionB和OptionC中在视图模型的定义如下面:

public final ObservableBoolean optionA = new ObservableBoolean(); 
public final ObservableBoolean optionB = new ObservableBoolean(); 
public final ObservableBoolean optionC = new ObservableBoolean(); 

这通常是足够的,但是如果要立即作出反应上点击,那么你可以添加回调和使用它们像:

OnPropertyChangedCallback userChoosedA = new OnPropertyChangedCallback() { 
    @Override 
    public void onPropertyChanged(Observable sender, int propertyId) { 
     (...) // basically propertyId can be ignored in such case 
    } 
}; 

optionA.addOnPropertyChangedCallback(userChoosedA); 

优势这样的方法之一是,你不需要COMPAR e并跟踪“id”。

相关问题