-
-
Save fnovoac/876a68439989e413444d6a7188068306 to your computer and use it in GitHub Desktop.
EditText validation in MVVM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> | |
<variable | |
name="model" | |
type="kr.changhoonjin.testmvp.view_models.Second"/> | |
</data> | |
<android.support.constraint.ConstraintLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context="kr.changhoonjin.testmvp.views.SecondActivity"> | |
<EditText | |
android:id="@+id/edit_name" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:hint="Name" | |
android:inputType="textPersonName" | |
android:text="@{model.name}" | |
app:addTextChangedListener="@{model.nameWatcher}" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent"/> | |
<EditText | |
android:id="@+id/edit_email" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:hint="Email" | |
android:inputType="textEmailAddress" | |
android:text="@{model.email}" | |
app:addTextChangedListener="@{model.emailWatcher}" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/edit_name"/> | |
<RatingBar | |
android:id="@+id/rating" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:rating="@{model.score}" | |
android:stepSize="1" | |
app:OnRatingBarChangeListener="@{model.ratingBarChangeListener}" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/edit_email"/> | |
<Button | |
android:id="@+id/btn1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:enabled="@{model.isValid}" | |
android:onClick="@{()->model.publish()}" | |
android:text="게시" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/rating"/> | |
</android.support.constraint.ConstraintLayout> | |
</layout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kr.changhoonjin.testmvp.view_models; | |
import android.databinding.ObservableBoolean; | |
import android.databinding.ObservableField; | |
import android.databinding.ObservableInt; | |
import android.text.Editable; | |
import android.text.TextUtils; | |
import android.text.TextWatcher; | |
import android.util.Log; | |
import android.util.Patterns; | |
import android.widget.RatingBar; | |
public class Second implements ViewModel { | |
public final ObservableField<String> name = new ObservableField<>(); | |
public final ObservableField<String> email = new ObservableField<>(); | |
public final ObservableInt score = new ObservableInt(); | |
public final ObservableBoolean isValid = new ObservableBoolean(); | |
@Override public void onCreate() { | |
score.set(0); | |
isValid.set(false); | |
} | |
@Override public void onResume() {} | |
@Override public void onPause() {} | |
@Override public void onDestroy() {} | |
private void validation() { | |
boolean isValidName = !TextUtils.isEmpty(name.get()); | |
boolean isValidEmail = !TextUtils.isEmpty(email.get()) && Patterns.EMAIL_ADDRESS.matcher(email.get()).matches(); | |
boolean isValidScore = score.get() > 0; | |
isValid.set(isValidName && isValidEmail && isValidScore); | |
} | |
public void publish() { | |
Log.w("==== [App] === Second | publish", "|" + "publish"); | |
} | |
/*************************** | |
* listener | |
***************************/ | |
public TextWatcher nameWatcher() { | |
return new TextWatcher() { | |
@Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} | |
@Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { | |
name.set(charSequence.toString()); | |
} | |
@Override public void afterTextChanged(Editable editable) { | |
validation(); | |
} | |
}; | |
} | |
public TextWatcher emailWatcher() { | |
return new TextWatcher() { | |
@Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} | |
@Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { | |
email.set(charSequence.toString()); | |
} | |
@Override public void afterTextChanged(Editable editable) { | |
validation(); | |
} | |
}; | |
} | |
public RatingBar.OnRatingBarChangeListener ratingBarChangeListener = (ratingBar, rating, fromUser) -> { | |
score.set((int) rating); | |
validation(); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment