安卓 - TextInputLayout无法包裹TextInputEditText的提示文本

4

我有一个简单的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>

但问题是,它并未显示任何内容。 Nothing 我必须将TIL的宽度设置为match_parent或者除wrap_content之外的其他任何宽度。但我希望宽度是动态的,即TIL的宽度跟随提供的提示长度。这是可能的吗?如果可以,应该如何操作?
我正在使用支持库版本为26.0.2
Yvette Colomb的答案很接近,TIL包裹了TIET,但那里没有奇特的动画效果。这可能是因为如果提示在执行时设置,则动画不会启动?

我不写任何Java代码,这只是一个测试布局。 - Kevin Murvie
尝试使用简单的EditText而不是TextInputEditText,并将EditTextHeight设置为matchparent。 - Tomin B Azhakathu
起初我使用的是EditText,并尝试了所有可能的match_parent - Kevin Murvie
你确切想要什么? - Ankita
你已经在高度属性中给父布局分配了layout_weight吗? - Sumit Kumar
@Ankita 我想要一个文本输入布局,它将跟随编辑文本中的提示来确定其宽度。我仍然希望保留提示在编辑文本上方移动的动画效果。 - Kevin Murvie
6个回答

6
你可以在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>

在活动中声明并初始化 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");

我已添加了一个浮动提示:

输入图片说明

当我们在输入时:

输入图片说明

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


哦,那几乎符合我的需求了!但是我还需要文本提示动画化,因为这样才能达到“今日学到”的目的 :( ... 我应该在问题中添加更多信息。 - Kevin Murvie
不,我指的是当用户输入字母时,可爱的动画会出现,并且提示会向上移动,这样我就不需要像普通表单那样在“ET”上面使用“TV”来显示用户正在输入哪个字段了。 - Kevin Murvie
哦!原来是不同的吗?我以为它们是一样的,因为这基本上就是 TIL 的作用哈哈,好的,我现在已经根据屏幕大小调整了 ET 的长度,所以暂时不需要再问问题了,谢谢! - Kevin Murvie
这并没有回答我的问题,但接近了,我以为点赞是我点的,所以我没有点赞哈哈,给你另一个赞! - Kevin Murvie

2
你可以从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

0
尝试这个:
<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 吗? - Kevin Murvie
@ShwetaChauhan 这会起作用,是的..我知道,但是..我问的是关于动态宽度TIL跟随其提示长度的问题,所以将其设置为match_parent没有帮助.. :( - Kevin Murvie

0
将您的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的大小,就像这样:按照提示的大小
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

0

我找到了一种解决方法,对于我来说,我想在一个禁用的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提示中添加更多字符,因为这是控制间距的内容。此外,约束布局只是因为我在活动中有其他元素,您可以避免使用它并使用任何根视图。

截图

enter image description here


-1

试着这样做。

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

这不起作用。我尝试过了,而且我刚刚又尝试了一次哈哈,还是不行:( - Kevin Murvie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接