簡體   English   中英

MVVM 數據綁定驗證

[英]MVVM data Binding validation

我想使用 MVVM 和數據綁定驗證注冊用戶數據。 這是我的代碼,請指導我:

活動

    registerViewModel = new ViewModelProvider(this).get(RegisterViewModel.class);
    binding.setSignUpObject(new RegisterRequest());
    binding.setSignUpClickListener(registerViewModel);

Model

public class RegisterRequest extends BaseObservable {


@SerializedName("Email")
private String userEmail;
@SerializedName("Password")
private String userPassword;
@Expose
private String userConfirmPassword;
@SerializedName("Phone")
private String userPhone;
@SerializedName("ImagePath")
private String imagePath;
@SerializedName("Name")
private String userName;

@Expose
private RegisterErrors registerErrors;
}

// 當然使用帶有 nonotifyPropertyChanged 的 setter 和帶有 @Bindable 注釋的 getter // 我創建了一個 RegisterErrors class 以在應用程序上使用它並出現錯誤:error xml

注冊錯誤

public class RegisterErrors {

private String userEmailError;
private String userPasswordError;
private String userConfirmPasswordError;
private String userPhoneError;
private String imagePathError;
private String userNameError;

}

XML

   <variable
        name="signUpObject"
        type="com.rabe7.community.model.request.register.RegisterRequest" />

    <variable
        name="signUpClickListener"
        type="com.rabe7.community.view_model.user_management.RegisterViewModel" />

                        <com.google.android.material.textfield.TextInputLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:textColorHint="@color/colorBlackTransparent">

                            <EditText
                                android:id="@+id/et_sign_up_user_name"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_marginEnd="@dimen/dp16w"
                                android:hint="@string/label_sign_up_user_name"
                                app:error="@{signUpObject.registerErrors.userNameError}"
                                android:inputType="text"
                                android:maxLines="1"
                                android:text="@={signUpObject.userName}"
                                android:textColor="@color/colorBlack" />

                        </com.google.android.material.textfield.TextInputLayout>

問題是..我想在單擊提交按鈕后驗證用戶輸入..如何在視圖 model 上通過綁定驗證它

我試試這個:

視圖模型

    public void onRegisterSubmitClicked(RegisterRequest registerRequest){
    this.registerRequest = registerRequest;

    if(registerRequest.getUserName().length()<6){
        registerRequest.getRegisterErrors().setUserNameError("error");
        }

}

但是app:error不起作用,我不知道該怎么做..所以請幫助我:)

您可以在 ViewModel 中這樣做:

 /**
     * Two way bind-able fields
     */
    var userName: String = "" 

另外,在 ViewModel 中編寫一個驗證屏幕的方法

fun validateSignupScreen() {
    if (userName.isEmpty()) {
        return
    } else if (userPassword.isEmpty()) {
        return
    } else {
        // Do your work here 
    }
}

在您的活動 class 中執行以下操作:

registerViewModel.validateSignupScreen()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM