android - 使文本计数器始终保持可见

标签 android counter android-textinputlayout textedit android-textinputedittext

问题:字符计数器在屏幕外运行

我发现很难有一个字符计数器,一旦输入文本滚动到一个屏幕尺寸上就不会消失。

这是我在布局 xml 中的内容:

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

<android.support.design.widget.TextInputLayout
    android:id="@+id/textContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:counterEnabled="true"
    android:overScrollMode="never"
    android.support.design:errorEnabled="true"
    android.support.design:hintEnabled="true"
    app:counterMaxLength="@integer/global_comments_size">

    <android.support.design.widget.TextInputEditText
    android:id="@+id/action_global_comment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="start|center_vertical"
    android:layout_marginTop="8dp"
    android:hint="@string/edit_comments"
    android:imeOptions="actionDone"
    android:inputType="textMultiLine"
    android:scrollbars="vertical"
    android:maxLength="@integer/global_comments_size"
    />
</android.support.design.widget.TextInputLayout>

</RelativeLayout>

counter visible until scroll leaves the screen

Counter becomes invisible as the text scroll over the screen

可能的选择:

我认为以下三个选项是可能的解决方案。

  1. 制作底部工具栏并以编程方式添加计数器

  2. 一旦达到最大尺寸,以编程方式制作弹出显示

  3. 以某种方式在 xml 中配置 TextInputLayout 或使用其他库。

我更喜欢选项 3。但是,我现在不知所措,既没有在文档中也没有通过谷歌搜索论坛找到任何解决方案或提示。


已解决

找到解决方案

经过更多阅读,我发现这个问题已经在 stackoverflow 上被问到 here关于用解决方案建议跑出屏幕的计数器。 然而,该解决方案并不像预期的那样使用简单的 xml 条目,而是必须以编程方式完成

我根据从 here 收集的信息发布带有源代码的解决方案和 here .

最佳答案

您所要做的就是像这样使用 alignParent

       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android.support.design="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.TextInputEditText
    android:id="@+id/action_global_comment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="start|center_vertical"
    android:layout_marginTop="8dp"
    android:hint="@string/edit_comments"
    android:imeOptions="actionDone"
    android:inputType="textMultiLine"
    android:maxLength="@integer/global_comments_size"
    android:layout_alignParentTop="true"
    android:layout_above="@+id/textContainer"
    android:scrollbars="vertical" />

<android.support.design.widget.TextInputLayout
    android:id="@+id/textContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:overScrollMode="never"
    android.support.design:errorEnabled="true"
    android.support.design:hintEnabled="true"
    app:counterEnabled="true"
    app:counterMaxLength="@integer/global_comments_size"/></RelativeLayout>

关于android - 使文本计数器始终保持可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52590045/

相关文章:

java - 如何添加重置按钮以清除 Edittext 字段上的数字

android - 如何配置 Dagger +gradle

Android:将 GPSTimeStamp 放入 jpg EXIF 标签中

android - 密码切换图标 textinputlayout 覆盖错误图标

android - 无法在 TextInputLayout 上扩充 ColorStateList

android - OnTouchListener 在 Fragment 中无法正常工作

javascript - 用计数器替换字符串并在 JavaScript 中递增计数器的最佳方法是什么?

counter - Dropwizard 度量如何在不结转计数器值的情况下对操作进行计数

linux - BeagleBoard 循环计数器

android - TextInputLayout 内的 EditText 在顶部留下空白区域