我被困在绑定适配器中。以下适配器无法正常工作,并且 IDE 似乎无法找到此适配器。我尝试了很多解决方案,但都是徒劳的。
我尝试了这个问题中提供的所有答案,但没有任何效果: Cannot find the setter for attribute with parameter
注意:我有另一个适配器可以在同一个文件中加载图像,它工作正常。
我想要实现的目标很简单,每当用户在某个editText中输入字符时,如果该editText指定了目的地,则在字符数达到限制后请求目的地焦点。
绑定适配器
@BindingAdapter(value = ["destination","limit"])
fun moveToNext(editText: EditText, dest: EditText, limit:Int) {
editText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (count == limit) {
dest.requestFocus()
}
}
})
}
在 .xml 中
<?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:bind="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.eshaafi.eshaafi.ui.auth.mobile_verification.MobileVerificationViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<ImageView
android:id="@+id/logo"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="48dp"
android:src="@drawable/ic_launcher_512"
android:transitionName="sharedLogo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.35"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/logoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="e s h a a f i"
android:textColor="@color/colorLogoBlue"
android:textSize="24sp"
android:textStyle="bold"
android:transitionName="sharedLogoText"
app:layout_constraintBottom_toBottomOf="@+id/logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.12"
app:layout_constraintStart_toEndOf="@+id/logo"
app:layout_constraintTop_toTopOf="@+id/logo" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Wait for verification code"
android:textColor="@color/colorBlack"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/logo" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="You will soon receive verification code on"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/mobileNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={viewModel.mobileNumber}"
android:textColor="@color/colorBlack"
android:textStyle="bold"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<EditText
android:id="@+id/digit_1"
style="@style/CodDigitEditText"
android:text=""
app:layout_constraintEnd_toStartOf="@+id/digit2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="@+id/mobileNumber"
destination="@{digit2}"
limit="1"
/>
<EditText
android:id="@+id/digit2"
style="@style/CodDigitEditText"
android:text=""
app:layout_constraintEnd_toStartOf="@+id/digit_3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/digit_1"
app:layout_constraintTop_toTopOf="@+id/digit_1" />
<EditText
android:id="@+id/digit_3"
style="@style/CodDigitEditText"
android:text=""
app:layout_constraintEnd_toStartOf="@+id/dash"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/digit2"
app:layout_constraintTop_toTopOf="@+id/digit_1" />
<View
android:id="@+id/dash"
android:layout_width="16dp"
android:layout_height="2dp"
android:background="@color/colorGrey"
app:layout_constraintEnd_toStartOf="@+id/digit_4"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/digit_3"
app:layout_constraintTop_toTopOf="@id/digit_1"
app:layout_constraintBottom_toBottomOf="@id/digit_1"/>
<EditText
android:id="@+id/digit_4"
style="@style/CodDigitEditText"
android:text=""
app:layout_constraintEnd_toStartOf="@+id/digit_5"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/dash"
app:layout_constraintTop_toTopOf="@+id/digit_1" />
<EditText
android:id="@+id/digit_5"
style="@style/CodDigitEditText"
android:text=""
app:layout_constraintEnd_toStartOf="@+id/digit_6"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/digit_4"
app:layout_constraintTop_toTopOf="@+id/digit_1" />
<EditText
android:id="@+id/digit_6"
style="@style/CodDigitEditText"
android:text=""
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/digit_5"
app:layout_constraintTop_toTopOf="@+id/digit_1"
android:imeOptions="actionDone"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/btnContinue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="64dp"
android:layout_marginRight="64dp"
android:layout_marginBottom="16dp"
android:alpha="0"
android:padding="16dp"
android:text="Continue"
android:textColor="@color/colorPrimary"
android:visibility="gone"
app:backgroundTint="@color/colorWhite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
错误者
Cannot find a setter for <android.widget.EditText destination> that accepts parameter type 'android.widget.EditText'