【问题标题】:how to set regex in EditText with Android two way binding?如何使用 Android 两种方式绑定在 EditText 中设置正则表达式?
【发布时间】:2021-06-01 06:26:53
【问题描述】:

您好,我想知道如何使用 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接收并处理,但我想不出更好的方法,所以我问了这个问题

你能介绍一下我吗?

【问题讨论】:

  • 这是一个完全不同的故事,我不是在问,因为我不能使用正则表达式,它是关于使用正则表达式的一种有效方式,并且是实时数据双向绑定

标签: kotlin mvvm


【解决方案1】:

我只是使用 android:onTextChanged 完成的,谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-28
    • 2015-01-31
    • 2021-08-28
    • 1970-01-01
    • 2012-05-06
    • 2020-11-19
    • 2023-01-17
    • 1970-01-01
    相关资源
    最近更新 更多