Android 带有 void 参数的数据绑定属性
Android Data Binding attribute with void argument
我为数据绑定创建了一个自定义属性,它不需要任何参数,它所做的只是验证 TextinputEditText 但我想不出在 xml 属性中传递 void 参数的方法.
这无法编译。
BindingAdapter.java
@BindingAdapter("app:validator")
public static void textValidator(TextInputLayout textInputLayout) {
doesSomething();
}
layout_file.xml
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:validator="@{ void }">
<android.support.design.widget.TextInputEditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"" />
</android.support.design.widget.TextInputLayout>
我试过在 xml 中传递 void 或 null,但它无法编译。
我研究发现方法中至少应该有一个或两个值参数。所以我可以通过传递一个参数来让它工作,比方说一个布尔值只是为了传递而不是使用它。例如,
这个编译。但这只是一个 hack,请有人提出更好的方法。
BindingAdapter.java
@BindingAdapter("app:validator")
public static void textValidator(TextInputLayout textInputLayout, boolean bl) {
doesSomething();
}
layout_file.xml
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:validator="@{ booleanVariable }">
<android.support.design.widget.TextInputEditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"" />
</android.support.design.widget.TextInputLayout>
But it's just a hack, someone please suggest a better approach.
没有更好的方法,因为这不是数据绑定的目的。它的目的是从字面上 绑定数据 ,这意味着应该有一些数据(一个参数)你想在你的视图上设置,而不必在你的代码中手动完成。
您尝试做的事情没有任何意义,因为您的自定义绑定只会在您传递要绑定到给定视图的数据时才会被评估。因此,您最终会得到传递一些虚拟值以触发验证的代码,此时您还不如自己手动触发验证,这比通过像这样通过数据绑定间接执行要清楚得多。
如果您想验证您的视图以响应另一个视图上的某些事件,比如 "submit button" 单击或其他内容,那么您可以将事件处理程序绑定到 that 控件,它又会调用您的 doesSomething()
方法。请参阅 section on event handling in the data binding documentation。
希望对您有所帮助。
你可以传递一个验证标准,比如一个枚举,表明它是否必须验证电子邮件、数字、最小长度字符串等,甚至是错误消息:
@BindingAdapter({"app:validation", "app:errorMsg"})
public static void setErrorEnable(EditText editText, StringValidationRules.StringRule stringRule, final String errorMsg)
我为数据绑定创建了一个自定义属性,它不需要任何参数,它所做的只是验证 TextinputEditText 但我想不出在 xml 属性中传递 void 参数的方法.
这无法编译。
BindingAdapter.java
@BindingAdapter("app:validator")
public static void textValidator(TextInputLayout textInputLayout) {
doesSomething();
}
layout_file.xml
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:validator="@{ void }">
<android.support.design.widget.TextInputEditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"" />
</android.support.design.widget.TextInputLayout>
我试过在 xml 中传递 void 或 null,但它无法编译。 我研究发现方法中至少应该有一个或两个值参数。所以我可以通过传递一个参数来让它工作,比方说一个布尔值只是为了传递而不是使用它。例如,
这个编译。但这只是一个 hack,请有人提出更好的方法。
BindingAdapter.java
@BindingAdapter("app:validator")
public static void textValidator(TextInputLayout textInputLayout, boolean bl) {
doesSomething();
}
layout_file.xml
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:validator="@{ booleanVariable }">
<android.support.design.widget.TextInputEditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"" />
</android.support.design.widget.TextInputLayout>
But it's just a hack, someone please suggest a better approach.
没有更好的方法,因为这不是数据绑定的目的。它的目的是从字面上 绑定数据 ,这意味着应该有一些数据(一个参数)你想在你的视图上设置,而不必在你的代码中手动完成。
您尝试做的事情没有任何意义,因为您的自定义绑定只会在您传递要绑定到给定视图的数据时才会被评估。因此,您最终会得到传递一些虚拟值以触发验证的代码,此时您还不如自己手动触发验证,这比通过像这样通过数据绑定间接执行要清楚得多。
如果您想验证您的视图以响应另一个视图上的某些事件,比如 "submit button" 单击或其他内容,那么您可以将事件处理程序绑定到 that 控件,它又会调用您的 doesSomething()
方法。请参阅 section on event handling in the data binding documentation。
希望对您有所帮助。
你可以传递一个验证标准,比如一个枚举,表明它是否必须验证电子邮件、数字、最小长度字符串等,甚至是错误消息:
@BindingAdapter({"app:validation", "app:errorMsg"})
public static void setErrorEnable(EditText editText, StringValidationRules.StringRule stringRule, final String errorMsg)