首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android,EditText issue issue焦点问题

Android,EditText issue issue焦点问题
EN

Stack Overflow用户
提问于 2022-07-15 10:29:01
回答 1查看 42关注 0票数 0

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

我已经搜索了一些解决方案,如果我的EditText在一个RecyclerView中,但是这些解决方案与我的项目不兼容。我怀疑这是因为我的EditText在RecyclerView中,而在CustomDialog中。

如果有人知道如何解决这个问题,那就太好了。

编辑:

这是包含CustomDialog的RecyclerView代码:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-20 19:57:01

我们将requestFocus()requestFocusFromTouch()放在一起,它起作用:

代码语言:javascript
复制
#personGasto is the editText
override fun afterTextChanged(p0: Editable?) {
                    #...
                    #some code
                    #...
                    personGasto.requestFocus()
                    personGasto.requestFocusFromTouch()
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72992600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档