简体   繁体   English

如何在 Android 工作室 Java 中的材料设计文本字段中添加字符计数器

[英]How to add a character counter to a material design textfield in Android studio Java

I am trying to add a character counter to a material design textfield, this is the textfield xml code:我正在尝试向材料设计文本字段添加字符计数器,这是文本字段 xml 代码:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/outlinedTextFieldDescripcionUniverso"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="370dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp"
    android:hint="@string/descripcion"
    android:textColorHint="@color/textos"
    app:counterEnabled="true"
    app:counterMaxLength="150"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/outlinedTextFieldNombreUniverso"> 

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/textInputEditTextDescripcionUniverso"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:textColor="@color/textos" />
</com.google.android.material.textfield.TextInputLayout>

this works nicely and the counter appears,这很好用,计数器出现,

textfield with counter带计数器的文本字段

but then i want to make the textfield to be vertically bigger, so it works as a textarea where you can write multiple lines and always has the same height.但后来我想让文本字段垂直更大,所以它作为一个文本区域工作,你可以在其中编写多行并且始终具有相同的高度。 So i change the layout height of textfield and textInputEditText, the code looks like this:所以我改变了textfield和textInputEditText的布局高度,代码如下:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/outlinedTextFieldDescripcionUniverso"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="370dp"
    android:layout_height="150dp"
    android:layout_marginTop="40dp"
    android:hint="@string/descripcion"
    android:textColorHint="@color/textos"
    app:counterEnabled="true"
    app:counterMaxLength="150"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/outlinedTextFieldNombreUniverso">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/textInputEditTextDescripcionUniverso"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:textColor="@color/textos" />
</com.google.android.material.textfield.TextInputLayout>

bigger textfield without counter没有计数器的更大文本字段

And now the character counter has dissapeared (im assuming its hidden under the textfield).现在字符计数器已经消失(我假设它隐藏在文本字段下)。 ¿Why doesn't it move to be at the end of the textfield? ¿ 为什么它不移动到文本字段的末尾?

¿is there any other way to do this? ¿有没有其他方法可以做到这一点? i have tried normal textfield and the same happens我试过普通的文本字段,同样的情况发生

I think it's because the TextInputLayout has a fixed height as I saw in your code android:layout_height="150dp"我认为这是因为 TextInputLayout 具有固定的高度,正如我在您的代码中看到的android:layout_height="150dp"

Solution Code:解决方案代码:

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/outlinedTextFieldDescripcionUniverso"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="370dp"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:hint="@string/descripcion"
android:textColorHint="@color/textos"
app:counterEnabled="true"
app:counterMaxLength="150"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/outlinedTextFieldNombreUniverso">

<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/textInputEditTextDescripcionUniverso"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:gravity="top"
    android:inputType="textMultiLine"
    android:textColor="@color/textos" />

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

Feel free to ask if something is unclear.随意询问是否有不清楚的地方。 And Kindly mark it as the correct answer if it helps you so that in future this answer could help any other needy.如果对您有帮助,请将其标记为正确答案,以便将来此答案可以帮助任何其他有需要的人。

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

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