我想在 repeatPasswordInputEditText 不為空時啟用按鈕,我嘗試啟用它android:enabled="@{!repeatPasswordInputEditText.text.toString().isEmpty()}"但它不起作用,為什么?我也在呼叫 binding.lifecycleOwner = thisonCreateView
<com.google.android.material.textfield.TextInputLayout
android:id="@ id/uRepeatPassword"
android:layout_width="384dp"
android:layout_height="75dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:hint="@string/repeat_password_hint"
app:passwordToggleEnabled="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/uNewPassword">
<com.google.android.material.textfield.TextInputEditText
android:id="@ id/repeatPasswordInputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:text="@{passwordChangeViewModel._repeatPassword}"
android:background="@color/white"
app:passwordToggleEnabled="false"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="@ id/changePasswordButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change Password"
android:enabled="@{!repeatPasswordInputEditText.text.toString().isEmpty()}"
app:layout_constraintTop_toBottomOf="@ id/uRepeatPassword">
</com.google.android.material.button.MaterialButton>
uj5u.com熱心網友回復:
您無法監聽EditTextXML 本身內部文本的變化。
在你的代碼中,你有
android:text="@{passwordChangeViewModel._repeatPassword}"
好像你要使用雙向資料系結在這些地方_repeatPassword是一個MutableLiveData<String>。這行不通,因為它現在只是一種方式。您需要替換@with@=以使其具有兩種方式。現在您有雙向資料系結作業,您可以使用此實時資料的值來啟用/禁用您的按鈕:
android:enabled="@{!_repeatPassword.empty}"
如果您不使用雙向資料系結,則必須將邏輯放在 Activity/Fragment 中:
repeatPasswordInputEditText.doAfterTextChanged { text ->
changePasswordButton.enabled = text.isNotEmpty()
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/365650.html
下一篇:加載微調器禁用HTML5必需屬性
