首页 > 解决方案 > 如何使用 Android 两种方式绑定在 EditText 中设置正则表达式?

问题描述

您好,我想知道如何使用 Android 双向绑定在 EditText 中设置正则表达式

我的正则表达式

val VALID_PASSWOLD_REGEX_ALPHA_NUM: Pattern = Pattern.compile("^(?=.*[A-Za-z])(?=.*[0-9])(?=.*[$@$!%*#?&])[A-Za-z[0-9]$@$!%*#?&]{8,32}$")

我的视图模型

val passwordText = MutableLiveData<String>("")

我的 xml

<EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:background="@null"
                android:onTextChanged="@{(text, start, before, count) -> signUpViewModel.onPasswordChanged(text)}"
                android:hint="@string/sign_up_password"
                android:text="@={signUpViewModel.passwordText}"
                android:textSize="22dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@id/name_text" />

我短暂的想法是应该从onTextChange接收并处理,但我想不出更好的方法,所以我问了这个问题

你能介绍我另一种方式吗?

标签: kotlinmvvm

解决方案


我刚刚使用 android:onTextChanged 谢谢


推荐阅读