简体   繁体   English

Android - TextInputLayout 不包装 TextInputEditText 的提示

[英]Android - TextInputLayout doesn't wrap hint of TextInputEditText

I have a simple TextInputEditText nested in TextTnputLayout .我有一个简单的TextInputEditText嵌套在TextTnputLayout

I am trying to make the of TIL to be as long as the hint given in TIET .我试图使TILTIET给出的提示一样长。

<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>

But the problem is, it doesn't show anything但问题是,它没有显示任何东西没什么

I have to set TIL 's width to match_parent or just any other width other than wrap_content .我必须将TIL的宽度设置为match_parent或除wrap_content之外的任何其他宽度。 But I want to the width to be dynamic as in, the width of TIL follows the length of the hint provided.但是我希望宽度是动态的,因为TIL的宽度遵循提供的提示的长度。 Is it possible?有可能吗? If yes, how to do that?如果是,如何做到这一点?

I am using support library version 26.0.2 btw.我正在使用支持库版本26.0.2顺便说一句。

Yvette Colomb's answer is close, the TIL wraps the TIET but the fancy animation doesn't work there. Yvette Colomb 的回答很接近, TIL包裹了TIET但精美的动画在那里不起作用。 It might be because the animation won't start if hints are set in execution?可能是因为如果在执行中设置了提示,动画将无法启动?

You can do so, by setting the hint in the java code.您可以通过在 java 代码中设置提示来做到这一点。

Remove the hint from the xml:从 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>

Declare and initialise the TextInputEditText in the activity and set the hint.在活动中声明并初始化 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");
}

在此处输入图片说明

This will wrap the text to the width of the hint as you type (not saying this is how it goes for all devices)这将在您键入时将文本包装到提示的宽度(不是说这是适用于所有设备的方式)

在此处输入图片说明

Then expand the width when you've entered the text.然后在输入文本后扩展宽度。

在此处输入图片说明

To answer your updated question.回答您更新的问题。

How to make the input layout grow as we enter input.如何让输入布局随着我们输入而增长。

<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"/>

The 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");

I've added a floating hint:我添加了一个浮动提示:

在此处输入图片说明

As we're typing:当我们打字时:

在此处输入图片说明

If you have any other questions about this, please ask a new question.如果您对此有任何其他问题,请提出一个新问题。

You can achieve this from xml instead, just set the hint for both TextInputLayout and TextInputEdittext and it will work then to make the TextInputEdittext grow as you are typing you have to set the it maxlines="1" and the layout_height to wrap_content as below您可以从 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

Try this:试试这个:

<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>

I found a type of workaround, for me I wanted to show Country Code in a disabled Material Design styled EditText, but same as you I was unable to use the wrap_context inside TextInputLayout.我找到了一种解决方法,对我来说,我想在禁用的 Material Design 样式的 EditText 中显示国家/地区代码,但与您一样,我无法在 TextInputLayout 中使用 wrap_context。

So as a workaround I added the hint text in both TextInputLayout and TextInputEditText, and since I had a predefined text inside the TextInputEditText, the hint of TextInputEditText never appeared on screen, but made space for the hint of TextInputLayout to be visible.因此,作为一种解决方法,我在 TextInputLayout 和 TextInputEditText 中添加了提示文本,并且由于我在 TextInputEditText 中有预定义的文本,因此 TextInputEditText 的提示从未出现在屏幕上,但为 TextInputLayout 的提示留出了空间可见。

Here is what I did...这是我所做的...

<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>

PS You might want to add a few more characters in the TextInputEditText hint, as that is what is controlling the spacing. PS 您可能希望在 TextInputEditText 提示中添加更多字符,因为这是控制间距的内容。 Also the constraint layout is there just because I had other elements in the activity, you can avoid it and use any of your root view.还有约束布局只是因为我在活动中有其他元素,您可以避免它并使用任何根视图。

Screenshot截图

在此处输入图片说明

set your xml like so:像这样设置你的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>

Now, adjust the size of the input_text_field to be BY THE SIZE OF THE HINT , depending on the hint size, like so:现在,根据提示大小将 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

Try doing this.尝试这样做。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM