Ume*_*wat 5 android 2-way-object-databinding android-databinding
我正在尝试使用单选按钮的两种方式数据绑定。它可以通过如下所示的一种方式正常工作,
android:checked="@{registration.gender.equals(Gender.FEMALE.getValue())}"。
但我的问题是,我需要在模型中设置所选单选按钮的值。
<?xml version="1.0" encoding="UTF-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="com.callhealth.customer.comman.enums.Gender" />
<import type="android.text.TextUtils" />
<import type="java.lang.Integer" />
<variable name="callback" type="com.callhealth.customer.usermanagement.callback.RegistrationCallback" />
<variable name="registration" type="com.callhealth.customer.usermanagement.model.request.LoginAndRegistrationRequestModel" />
</data>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/label_gender" android:textSize="15sp" />
<RadioGroup android:id="@+id/gender_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:orientation="horizontal">
<android.support.v7.widget.AppCompatRadioButton android:layout_width="wrap_content" android:checked="@={registration.gender.equals(Gender.MALE.getValue())}" android:layout_height="wrap_content" android:text="@string/label_male" />
<android.support.v7.widget.AppCompatRadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginStart="20dp" android:checked="@={registration.gender.equals(Gender.FEMALE.getValue())}" android:text="@string/label_female" />
</RadioGroup>
</LinearLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
我的模特班
public class LoginAndRegistrationRequestModel extends BaseObservable {
private Integer gender;
@Bindable
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
notifyPropertyChanged(BR.gender);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用时
android:checked="@={registration.gender.equals(Gender.FEMALE.getValue())}"
Run Code Online (Sandbox Code Playgroud)
Gradel 抛出错误
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:The expression registrationGender.equals(com.callhealth.customer.comman.enums.Gender.MALE.getValue()) cannot be inverted: There is no inverse for method equals, you must add an @InverseMethod annotation to the method to indicate which method should be used when using it in two-way binding expressions
file:S:\Umesh\android\android_studio_workspace\CallHealth\app\src\main\res\layout\content_user_registration.xml
loc:148:48 - 148:97
****\ data binding error ****
Run Code Online (Sandbox Code Playgroud)
几个小时后,我找到了一个简单的方法:android 中的双向数据绑定。它是一个带有 livedata 和 Kotlin 的基本框架。另外,您可以使用 ObservableField()
布局.xml
<data>
<variable
name="VM"
type="...YourViewModel" />
</data>
<LinearLayout
android:id="@+id/settings_block_env"
...
>
<RadioGroup
android:id="@+id/env_radioGroup"
android:checkedButton="@={VM.radio_checked}">
<RadioButton
android:id="@+id/your_id1"/>
<RadioButton
android:id="@+id/your_id2" />
<RadioButton
android:id="@+id/your_id3"/>
<RadioButton
android:id="@+id/your_id4"/>
</RadioGroup>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
class YourViewModel(): ViewModel {
var radio_checked = MutableLiveData<Int>()
init{
radio_checked.postValue(R.id.your_id1)//def value
}
//other code
}
Run Code Online (Sandbox Code Playgroud)
您将其绑定到boolean表达式。设置时它不知道要调用什么方法。我会尝试将您的财产设为boolean(例如isFemale)。听起来还有一种方法可以用 @InverseMethod 注释来指示 setter,但我没有使用过,而且在我看来,这种boolean方法会更直接。如果您想引用 java 代码中的其他位置,您始终可以根据字段boolean来实现属性。Integer genderInteger
| 归档时间: |
|
| 查看次数: |
9042 次 |
| 最近记录: |