kotlin 无法使用TextInputLayout更新Firestore中的数据

pzfprimi  于 2023-01-31  发布在  Kotlin
关注(0)|答案(1)|浏览(146)

我在“编辑配置文件”屏幕上使用TextInputLayout,我想更新该屏幕中的数据,但当我使用该屏幕时,数据更新为喜欢
“网站.谷歌.安卓.材料.文本域.文本输入布局{9544 eb 3版本编辑93,232 - 931,455 #7f0a00d8应用程序:id/etEditName aid=1073741880}”
我会留下密码。

配置文件片段.kt

....
    
    binding.btnEditProfile.setOnClickListener {
            val dialog =
                LayoutInflater.from(context).inflate(R.layout.edit_profile_dialog, null)
            val builder = AlertDialog.Builder(context).setView(dialog).show()
            builder.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            builder.setCancelable(true)

            val etName = dialog.findViewById<TextInputLayout>(R.id.etEditName)
            etName.editText?.setText(user.name)
            val etSurname = dialog.findViewById<TextInputLayout>(R.id.etEditSurname)
            etSurname.editText?.setText(user.surname)
            val etWeight = dialog.findViewById<TextInputLayout>(R.id.etEditWeight)
            etWeight.editText?.setText(user.weight)
            val etHeight = dialog.findViewById<TextInputLayout>(R.id.etEditHeight)
            etHeight.editText?.setText(user.height)
            val etGoal = dialog.findViewById<TextInputLayout>(R.id.etEditGoal)
            etGoal.editText?.setText(user.calorieGoal.toString())

            dialog.findViewById<Button>(R.id.btnEditProfile).setOnClickListener {
                if (etSurname.isNotEmpty() && etHeight.isNotEmpty() && etWeight.isNotEmpty() && etGoal.isNotEmpty())
                    dbUser.document(auth.currentUser?.email.toString())
                        .update(
                            mapOf(
                                "name" to etName.toString(),
                                "surname" to etSurname.toString(),
                                "height" to etHeight.toString(),
                                "weight" to etWeight.toString(),
                            )
                        )
                        .addOnSuccessListener {
                            Toast.makeText(context, "Updated!!", Toast.LENGTH_SHORT).show()
                            builder.dismiss()
                        } else {
                    Toast.makeText(context, "Fill in the fields!!", Toast.LENGTH_SHORT).show()
                }
            }
        }

编辑对话框.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">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="36dp"
    android:background="@drawable/dialog_bg">

    <TextView
        android:id="@+id/txtEditProfile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:fontFamily="monospace"
        android:text="@string/edit_profile"
        android:textColor="@color/primaryDarkColor"
        android:textSize="32sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/etEditName"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="24dp"
        android:layout_marginTop="24dp"
        android:hint="@string/enter_name"
        android:maxLines="1"
        app:errorEnabled="true"
        app:hintTextColor="@color/gray"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/txtEditProfile">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inputType="textPersonName"
            android:textColorHint="@color/primaryDarkColor" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/etEditSurname"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="24dp"
        android:layout_marginTop="24dp"
        android:hint="@string/enter_surname"
        android:maxLines="1"
        app:errorEnabled="true"
        app:hintTextColor="@color/gray"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/etEditName">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inputType="textPersonName"
            android:textColorHint="@color/primaryDarkColor" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/etEditWeight"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="110dp"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="24dp"
        android:layout_marginTop="24dp"
        android:focusableInTouchMode="false"
        android:hint="@string/weight"
        android:maxLines="1"
        android:text=""
        app:errorEnabled="true"
        app:layout_constraintEnd_toStartOf="@+id/etEditHeight"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/etEditSurname">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:cursorVisible="false"
            android:inputType="number"
            android:textColorHint="@color/primaryDarkColor" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/etEditGoal"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="24dp"
        android:layout_marginTop="8dp"
        android:hint="@string/calorie_goal"
        android:maxLines="1"
        android:text=""
        app:errorEnabled="true"
        app:hintTextColor="@color/gray"
        app:layout_constraintEnd_toStartOf="@+id/etEditHeight"
        app:layout_constraintHorizontal_bias="0.508"
        app:layout_constraintStart_toEndOf="@+id/etEditWeight"
        app:layout_constraintTop_toBottomOf="@id/etEditHeight">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:cursorVisible="false"
            android:inputType="number"
            android:textColorHint="@color/primaryDarkColor" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/etEditHeight"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="110dp"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="24dp"
        android:layout_marginTop="24dp"
        android:hint="@string/height"
        android:maxLines="1"
        android:text=""
        app:errorEnabled="true"
        app:hintTextColor="@color/gray"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/etEditWeight"
        app:layout_constraintTop_toBottomOf="@id/etEditSurname">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:cursorVisible="false"
            android:inputType="number"
            android:textColorHint="@color/primaryDarkColor" />
    </com.google.android.material.textfield.TextInputLayout>

    <Button
        android:id="@+id/btnEditProfile"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_margin="24dp"
        android:text="@string/submit"
        android:textColor="@color/secondaryTextColor"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/etEditGoal" />

</androidx.constraintlayout.widget.ConstraintLayout>

谢谢你的帮助:)

ubof19bj

ubof19bj1#

当您需要更新TextInputLayout内部EditText中的文本时,您正在更新TextInputLayout本身。

"name" to etName.toString(),
"surname" to etSurname.toString(),
"height" to etHeight.toString(),
"weight" to etWeight.toString(),

"name" to etName.editText?.text.toString(),
"surname" to etSurname.editText?.text.toString(),
"height" to etHeight.editText?.text.toString(),
"weight" to etWeight.editText?.text.toString(),

这将更新“编辑配置文件”屏幕中的用户数据。

相关问题