【问题标题】:Android - TextInputLayout doesn't wrap hint of TextInputEditTextAndroid - TextInputLayout 不包含 TextInputEditText 的提示
【发布时间】:2018-03-15 10:02:12
【问题描述】:

我有一个简单的TextInputEditText 嵌套在TextTnputLayout 中。

我正在尝试使TIL 的长度与TIET 中给出的提示一样长。

<android.support.design.widget.TextInputLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="This is a hint"/>

</android.support.design.widget.TextInputLayout>

但问题是,它没有显示任何内容

我必须将TIL 的宽度设置为match_parentwrap_content 以外的任何其他宽度。但我希望宽度是动态的,TIL 的宽度遵循提供的提示的长度。可能吗?如果是,该怎么做?

我正在使用支持库版本26.0.2 btw。

Yvette Colomb 的答案很接近,TIL 包裹了 TIET,但精美的动画在那里不起作用。可能是因为在执行中设置了提示,动画不会开始?

【问题讨论】:

  • 我不会用 Java 写任何东西,这是一个测试布局
  • 尝试简单的 EditText 插入 TextInputEditText 并设置 EditTextHeight 匹配父项
  • 起初我使用EditText,并尝试了所有可能的match_parent
  • 你到底想要什么?
  • 您是否在 height 属性中将 layout_weight 赋予其父布局??

标签: android android-textinputlayout


【解决方案1】:

像这样设置你的xml:

<com.google.android.material.textfield.TextInputLayout
     android:id="@+id/text_field_layout"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textSize="15sp">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/input_text_field"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        />

</com.google.android.material.textfield.TextInputLayout>

现在,根据提示大小将 input_text_field 的大小调整为 BY THE SIZE OF THE HINT,如下所示:

val tf = input_text_field
val layout = text_field_layout
val hint = "Hint"

val measureText = tf.paint.measureText(hint).toInt()
tf.width = tf.paddingLeft + tf.paddingRight + measureText.toInt()
layout.hint = hint

【讨论】:

    【解决方案2】:

    我找到了一种解决方法,对我来说,我想在禁用的 Material Design 样式的 EditText 中显示国家代码,但和你一样,我无法在 TextInputLayout 中使用 wrap_context。

    因此,作为一种解决方法,我在 TextInputLayout 和 TextInputEditText 中都添加了提示文本,并且由于我在 TextInputEditText 中有预定义的文本,因此 TextInputEditText 的提示从未出现在屏幕上,而是为 TextInputLayout 的提示留出了空间可见。

    这就是我所做的......

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:padding="16dp">
        ...
        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/country_code"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Country code"
            app:hintTextColor="#737373"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent">
    
            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="+91"
                android:hint="Country Code"
                android:enabled="false"
                android:textColor="#737373"
                .../>
    
        </com.google.android.material.textfield.TextInputLayout>
        ...
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    附:您可能希望在 TextInputEditText 提示中添加更多字符,因为这是控制间距的原因。约束布局也存在只是因为我在活动中有其他元素,您可以避免它并使用您的任何根视图。

    截图

    【讨论】:

      【解决方案3】:

      您可以从 xml 实现这一点,只需为 TextInputLayout 和 TextInputEdittext 设置提示,然后它会起作用,然后在您键入时使 TextInputEdittext 增长,您必须将它设置为 maxlines="1" 和 layout_height 为 wrap_content如下

       <android.support.design.widget.TextInputLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:hint="This is edittext">
      
                  <android.support.design.widget.TextInputEditText
                      android:hint="This is edittext
                      android:maxLines="1"
                      android:inputType="numberDecimal"
                      android:layout_width="wrap_content"
                      android:layout_height="match_parent" />
      
              </android.support.design.widget.TextInputLayout>
      
      I hope this help someone
      

      【讨论】:

        【解决方案4】:

        您可以通过在 java 代码中设置提示来做到这一点。

        从 xml 中删除提示:

        <android.support.design.widget.TextInputLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        
            <android.support.design.widget.TextInputEditText
                android:id="@+id/inputTxt"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        
        </android.support.design.widget.TextInputLayout>
        

        在activity中声明并初始化TextInputEditText并设置提示。

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            final TextInputEditText inputTxt = (TextInputEditText) findViewById(R.id.inputTxt);
            inputTxt.setHint("This is a hint");
        }
        

        这将在您键入时将文本换行到提示的宽度(并不是说这对所有设备都是如此)

        然后在你输入文本后扩大宽度。

        回答您更新的问题。

        如何让输入布局随着我们输入输入而增长。

        <android.support.design.widget.TextInputLayout
            android:id="@+id/txtInputL"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        
            <android.support.design.widget.TextInputEditText
                android:id="@+id/inputTxt"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:maxLines="1"/>
        

        Java。

        final TextInputEditText inputTxt = (TextInputEditText) findViewById(R.id.inputTxt);
        inputTxt.setHint("This is a hint");
        final TextInputLayout txtInputL = (TextInputLayout) findViewById(R.id.txtInputL);
        txtInputL.setMinimumWidth(300);
        txtInputL.setHint("This is a hint");
        

        我添加了一个浮动提示:

        我们正在输入:

        如果您对此有任何其他问题,请提出新问题。

        【讨论】:

        • 哦,这几乎完全符合我的需要!不过,我还需要对文本提示进行动画处理,因为这违背了TIL 的目的 :(.. 我应该在问题中添加更多信息
        • 不,当用户输入一个字母并且提示向上移动时,我不需要使用TV 上面的ET 来向用户显示他们正在输入的字段,就像普通表单一样..
        • 哦!所以不一样??我认为它是一样的,因为这基本上是 TIL 所做的,哈哈,好吧,我现在已经根据屏幕大小调整了 ETs 的长度,所以现在不需要另一个问题,谢谢!
        • 这没有回答我的问题,但它很接近,我认为赞成票是我的,所以我没有赞成票大声笑,在这里,再投票一次!
        【解决方案5】:

        试试这个:

        <android.support.design.widget.TextInputLayout
                        android:id="@+id/txt_inputLayout_gify"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
        
                        <android.support.design.widget.TextInputEditText
                            android:id="@+id/edt_gift_name"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="This is hint." />
                    </android.support.design.widget.TextInputLayout>
        

        【讨论】:

        • 我必须把TIL变成match_parent
        • @ShwetaChauhan 它会起作用,是的.. 我知道,但是.. 我在提示长度之后询问了动态宽度 TIL,所以将其设为 match_parent 并没有帮助..: (
        【解决方案6】:

        尝试这样做。

         <android.support.design.widget.TextInputEditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="This is a hint"/>
        

        【讨论】:

        • 这不起作用..我试过了,我又试了一次,哈哈,它仍然不起作用:(
        猜你喜欢
        • 2017-04-18
        • 2020-09-28
        • 1970-01-01
        • 1970-01-01
        • 2016-10-16
        • 1970-01-01
        • 2019-10-22
        • 2020-01-20
        • 1970-01-01
        相关资源
        最近更新 更多