设置提示后,EditText不会扩展

时间:2019-12-31 02:31:00

标签: android xml android-layout android-edittext android-constraintlayout

因此,我在EditText中有一个简单的ConstraintLayout。我想要的是让用户开始键入内容,然后EditText展开以适合他们的输入,这可以按预期工作,直到我添加提示为止。然后,视图似乎被限制为提示的宽度,而输入则完全没有变化。

在此先感谢您的帮助,如果有人需要,我将在下面发布完整的布局文件!

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/add_menu_item_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:title="@string/add_menu_item_title" />

        <EditText
            android:inputType="textCapWords"
            android:id="@+id/add_menu_item_name"
            style="@style/BlockListItem"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin"
            android:layout_marginTop="@dimen/margin"
            android:layout_marginEnd="@dimen/margin"
            android:textColorHint="@android:color/darker_gray"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/add_menu_item_toolbar" />

        <View
            android:id="@+id/add_menu_item_stock_frame"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="@dimen/margin"
            android:layout_marginTop="@dimen/margin"
            android:layout_marginEnd="@dimen/margin"
            android:background="@drawable/frame_border"
            app:layout_constraintBottom_toBottomOf="@id/add_menu_item_frame_margin"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/add_menu_item_name" />

        <LinearLayout
            android:id="@+id/add_menu_item_stock_container"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin"
            android:orientation="vertical"
            app:layout_constraintEnd_toEndOf="@id/add_menu_item_stock_frame"
            app:layout_constraintStart_toStartOf="@id/add_menu_item_stock_frame"
            app:layout_constraintTop_toTopOf="@id/add_menu_item_stock_frame" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/add_menu_item_add_stock"
            style="@style/Widget.MaterialComponents.Button.TextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin"
            android:layout_marginEnd="@dimen/margin"
            android:text="@string/add_menu_item_add_stock"
            app:icon="@drawable/baseline_add_24"
            app:layout_constraintBottom_toBottomOf="@id/add_menu_item_stock_frame"
            app:layout_constraintEnd_toEndOf="@id/add_menu_item_stock_frame"
            app:layout_constraintTop_toBottomOf="@id/add_menu_item_stock_container" />

        <Space
            android:id="@+id/add_menu_item_frame_margin"
            android:layout_width="match_parent"
            android:layout_height="@dimen/margin"
            app:layout_constraintTop_toBottomOf="@id/add_menu_item_add_stock" />

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/add_menu_item_price_container"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin"
            android:layout_marginTop="@dimen/margin"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/add_menu_item_stock_frame"
            app:layout_goneMarginTop="@dimen/margin">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/add_menu_item_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="6"
                android:hint="@string/menu_add_price_hint"
                android:inputType="numberDecimal" />
        </com.google.android.material.textfield.TextInputLayout>

        <TextView
            android:id="@+id/add_menu_item_suggested_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin"
            app:layout_constraintBottom_toBottomOf="@id/add_menu_item_price_container"
            app:layout_constraintStart_toEndOf="@id/add_menu_item_price_container"
            app:layout_constraintTop_toTopOf="@id/add_menu_item_price_container" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/add_menu_item_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/margin"
            android:text="@string/menu_add_add"
            app:layout_constraintBottom_toBottomOf="@id/add_menu_item_price_container"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@id/add_menu_item_price_container" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

2 个答案:

答案 0 :(得分:0)

只要您在edittext中有一个字符串,就可以简单地删除并重置提示:

add_menu_item_name.addTextChangedListener(object : TextWatcher {
            val hintText = "myHint"
            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) {
                add_menu_item_name.hint =
                    if (count == 0) hintText
                    else null
            }
        }
)

答案 1 :(得分:-4)

似乎一开始这只是一个愚蠢的错误,因为wrap_content并未按原本设置在视图上!