android中的双向数据绑定
假设我有一个登录表单,等待用户输入一个10位数的电话号码,然后它会更改next的按钮可见性 如何在没有代码的XML中实现这一点? 以下是电话检查功能:android中的双向数据绑定,android,data-binding,kotlin,android-databinding,two-way-binding,Android,Data Binding,Kotlin,Android Databinding,Two Way Binding,假设我有一个登录表单,等待用户输入一个10位数的电话号码,然后它会更改next的按钮可见性 如何在没有代码的XML中实现这一点? 以下是电话检查功能: fun EditText.isValidPhone() = this.text.length == 10 和布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="match_parent" android:l
fun EditText.isValidPhone() = this.text.length == 10
和布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<EditText
android:id="@+id/phoneEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:background="@null"
android:focusableInTouchMode="true"
android:hint="@string/mobile_phone"
android:inputType="number"
android:maxLength="10"
android:textColor="@color/regular_text"
android:textColorHint="@color/hint"
android:textSize="24sp" />
<TextView
android:id="@+id/authorization_next"
style="@style/accept_button"
android:text="@string/next"
android:visibility="gone" />
</LinearLayout>
生成的数据绑定Java类不知道扩展方法,因此必须导入包含静态扩展方法的适当类。假设文件名为extensions.kt,则该类名将为
ExtensionsKt
,您还必须将View
导入到绑定中。您可以使用onTextChanged
属性定义回调和文本更改的条件
android:onTextChanged="@{(text, start, before, count) -> authorizationNext.setVisibility(ExtensionsKt.isValidPhone(phoneEdit) ? View.VISIBLE : View.GONE)}" />
也许只为CharSequence
定义一个扩展名会很有用
fun CharSequence.isValidPhone() = length == 10
并将其用作扩展名kt.isValidPhone(text)。您使用的是Android数据绑定库还是任何其他工具?来自谷歌的Android数据绑定