簡體   English   中英

Android-Studio,CustomDialog焦點問題內RecyclerView內的EditText

[英]Android-Studio, EditText inside RecyclerView inside CustomDialog focus issue

我的焦點有問題。 每次單擊 EditText 時,我都可以寫 1 個字符,然后它就會失去焦點。

如果我的 EditText 在 RecyclerView 中,我已經搜索過,我找到了一些解決方案,但這些解決方案不適用於我的項目。 我懷疑這是因為我的 EditText 在 RecyclerView 內,而 RecyclerView 在 CustomDialog 內。

如果有人知道如何解決這個問題,那就太好了: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 的 View,包含 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>

我們已經解決了:D

我們將 requestFocus() 和 requestFocusFromTouch() 放入,它可以工作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM