Android-Studio, EditText在RecyclerView在CustomDialog焦点问题



我对焦点有问题。每次我点击EditText,我可以写1个字符,然后它失去焦点。

我已经搜索了,我发现了一些解决方案,如果我的EditText是在一个回收视图内,但那些不与我的项目工作。我怀疑这是因为我的EditText在一个CustomDialog中的RecyclerView中。

如果有人知道如何解决这个问题就太好了:D

编辑:

这是包含RecyclerView的CustomDialog的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foregroundTint="#009688">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/nombreGasto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Nombre"
android:inputType="textPersonName"
android:minHeight="48dp" />
<EditText
android:id="@+id/precioGasto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Precio"
android:inputType="numberDecimal"
android:minHeight="48dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerGasto"
android:layout_width="match_parent"
android:layout_height="145dp" />
<Space
android:layout_width="match_parent"
android:layout_height="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:backgroundTint="#FFFFFF"
android:text="Cancel"
android:textColor="#00BCD4" />
<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:backgroundTint="#FFFFFF"
android:text="OK"
android:textColor="#00BCD4" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

这是CardView的xml代码,它是接收RecyclerView的视图,它包含EditText:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/linearLay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/personNamePrecio"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="PersonaX"
android:textAlignment="viewStart"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editTextPrecioPersona"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="0.00"
android:inputType="numberDecimal"
android:minHeight="48dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

我们输入requestFocus()requestFocusFromTouch(),它工作:

#personGasto is the editText
override fun afterTextChanged(p0: Editable?) {
#...
#some code
#...
personGasto.requestFocus()
personGasto.requestFocusFromTouch()
}