简体   繁体   中英

Android Data Binding attribute with void argument

I made a custom attribute for data binding which doesn't require any argument and all it does is that valides the TextinputEditText but I can't figure out a way to pass void parameter in the xml attribute.

This doesn't compile.

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>

I've tried passing void or null in the xml but it just doesn't compile. I researched and found out that there should be atleast one or two value parameters in the method. So I could make it work by passing a parameter, let's say a boolean just for the sake of passing and not using it. For ex,

This one compiles. But it's just a hack, someone please suggest a better approach.

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.

There is no better approach because this is not what data binding is intended to do. Its intention is to literally bind data , which means there should be some piece of data (an argument) that you want to set on your view without having to manually do it in your code.

What you are attempting to do doesn't really make sense as your custom binding will only be evaluated when you pass data to be bound to the given view. So you would end up with code that passes some dummy value just to trigger validation, at which point you might as well just manually trigger the validation yourself, which would be much clearer than doing it indirectly through data binding like this.

If you want to validate your view in response to some event on another view, say a "submit button" click or something, then you could bind an event handler to that control, which in turn would call your doesSomething() method. See the section on event handling in the data binding documentation for that.

Hope that helps.

you could pass a validation criteria, like an enum indicating if it must validate an email, a number, a min lengh string, etc. or even an error message:

@BindingAdapter({"app:validation", "app:errorMsg"})
    public static void setErrorEnable(EditText editText, StringValidationRules.StringRule stringRule, final String errorMsg)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM