无法使用数据绑定从 xml 调用方法 ViewModel isValidate()
Cannot call method ViewModel isValidate() from xml with dataBinding
使用绑定表达式可以正常禁用按钮,但是当它很长时我需要将它移动到 ViewModel 中的方法,这就是它无法正常工作的地方。
<com.google.android.material.button.MaterialButton
android: id = "@ + id / btValidateLegalRepresentativeCE"
style = "@ style / BasicMaterialButton"
android: layout_width = "match_parent"
android: layout_marginTop = "21dp"
android: enabled = "@ {viewModel.documentLiveData.length()> 0 & viewModel.firstNameLiveData.length()> 0 & viewModel.lastNameLiveData.length()> 0} "
来自 ViewModel 的方法调用
<com.google.android.material.button.MaterialButton
android: id = "@ + id / btValidateLegalRepresentativeCE"
style = "@ style / BasicMaterialButton"
android: layout_width = "match_parent"
android: layout_marginTop = "21dp"
app: enabled = "@ {viewModel.isValidate()}" />
视图模型
private val _documentLiveData = MutableLiveData("")
val documentLiveData: MutableLiveData<String> get() = _documentLiveData
private val _firstNameLiveData = MutableLiveData("")
val firstNameLiveData: LiveData<String> get() = _firstNameLiveData
private val _lastNameLiveData = MutableLiveData("")
val lastNameLiveData: LiveData<String> get() = _lastNameLiveData
fun isValidate(): Boolean {
val document = documentLiveData.value?.length ?: 0
val firstName = firstNameLiveData.value?.length ?: 0
val lastName = lastNameLiveData.value?.length ?: 0
return (document > 0) && (firstName > 0) && (lastName > 0)
}
我考虑如下:
binding.viewModel = viewModel
binding.lifecycleOwner = this
感谢您的帮助。
要在视图模型中调用函数,您需要使用 ()->
app:enabled = "@ {()->viewModel.isValidate()}
如果上述方法不起作用,请创建自定义绑定适配器。
在java中,会像
@androidx.databinding.BindingAdapter("android:viewModel")
public static void setButtonEnable(Button button,YourViewModel viewModel) {
if(viewModel.isValidate())
//if true ,enable button
button.setEnable(true);
else
//disable button
button.setEnable(false);
}
从布局中调用此自定义适配器并将视图模型传递给适配器。
<com.google.android.material.button.MaterialButton
android: id = "@ + id / btValidateLegalRepresentativeCE"
style = "@ style / BasicMaterialButton"
android: layout_width = "match_parent"
android: layout_marginTop = "21dp"
android:viewModel= "@{viewModel}" />
使用绑定表达式可以正常禁用按钮,但是当它很长时我需要将它移动到 ViewModel 中的方法,这就是它无法正常工作的地方。
<com.google.android.material.button.MaterialButton
android: id = "@ + id / btValidateLegalRepresentativeCE"
style = "@ style / BasicMaterialButton"
android: layout_width = "match_parent"
android: layout_marginTop = "21dp"
android: enabled = "@ {viewModel.documentLiveData.length()> 0 & viewModel.firstNameLiveData.length()> 0 & viewModel.lastNameLiveData.length()> 0} "
来自 ViewModel 的方法调用
<com.google.android.material.button.MaterialButton
android: id = "@ + id / btValidateLegalRepresentativeCE"
style = "@ style / BasicMaterialButton"
android: layout_width = "match_parent"
android: layout_marginTop = "21dp"
app: enabled = "@ {viewModel.isValidate()}" />
视图模型
private val _documentLiveData = MutableLiveData("")
val documentLiveData: MutableLiveData<String> get() = _documentLiveData
private val _firstNameLiveData = MutableLiveData("")
val firstNameLiveData: LiveData<String> get() = _firstNameLiveData
private val _lastNameLiveData = MutableLiveData("")
val lastNameLiveData: LiveData<String> get() = _lastNameLiveData
fun isValidate(): Boolean {
val document = documentLiveData.value?.length ?: 0
val firstName = firstNameLiveData.value?.length ?: 0
val lastName = lastNameLiveData.value?.length ?: 0
return (document > 0) && (firstName > 0) && (lastName > 0)
}
我考虑如下:
binding.viewModel = viewModel
binding.lifecycleOwner = this
感谢您的帮助。
要在视图模型中调用函数,您需要使用 ()->
app:enabled = "@ {()->viewModel.isValidate()}
如果上述方法不起作用,请创建自定义绑定适配器。
在java中,会像
@androidx.databinding.BindingAdapter("android:viewModel")
public static void setButtonEnable(Button button,YourViewModel viewModel) {
if(viewModel.isValidate())
//if true ,enable button
button.setEnable(true);
else
//disable button
button.setEnable(false);
}
从布局中调用此自定义适配器并将视图模型传递给适配器。
<com.google.android.material.button.MaterialButton
android: id = "@ + id / btValidateLegalRepresentativeCE"
style = "@ style / BasicMaterialButton"
android: layout_width = "match_parent"
android: layout_marginTop = "21dp"
android:viewModel= "@{viewModel}" />