使文字计数器始终可见

时间:2018-10-01 11:24:12

标签: 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条目所期望的那样,而是必须以编程方式完成

我将根据从herehere收集的信息发布带有源代码的解决方案。

2 个答案:

答案 0 :(得分:2)

所有您需要做的就是使用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>

答案 1 :(得分:1)

我通过阅读文档和stackoverflow找到了解决方案。 我在这里发布了一个完整的示例,以使其更容易重现该行为。

不幸的是,似乎没有一种简单的配置xml属性的方法,该属性会使计数器停留在屏幕的可见区域。

解决方案在于使用接口类TextWatchers,幸运的是,只需使用几行代码即可。

1)创建 res / values / strings.xml 文件以定义最大字符大小的常量

<integer name="max_comment_size">50</integer>

2)在您的活动中添加以下两个视图 即res / layout / activity_main.xml

我喜欢把柜台放在最上面。如果您希望底部有TextView和TextEdit,则可能需要交换它。

<TextView
   android:id="@+id/constraint_textview"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Remaining characters/Maximum characters: --/--" />

<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textMultiLine"
    android:maxLength="@integer/max_comment_size" />

请注意,textMultiLine不会限制EditText字段的最大行数。它仅限制布局大小。 使用maxLength,您可以指定允许的最大字符数。

3)在Java文件中使用TextWatcher对输入的字符进行计数并在TextView中显示它们 在我的示例中, Java文件为:java / com / example / mycompany / textcounter / MainActivity.java

接口类TextWatcher具有三种需要实现的方法。但是,出于这个目的,我们只对onTextChange()感兴趣。

    public class MainActivity extends AppCompatActivity {

        private TextView mTextView;
        private EditText mEditText;
        private final TextWatcher mTextEditorWatcher = new TextWatcher() {
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {  } //nothing to be done here

            public void onTextChanged(CharSequence s, int start, int before, int count) {        
                Integer max_comment = getResources().getInteger(R.integer.max_comment_size);

                mTextView.setText("Remaining characters" + "/Maximum characters: "+ String.valueOf(max_comment-s.length()) +"/" + max_comment);
            }

            public void afterTextChanged(Editable s) { } //nothing to be done here
        };

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            //Initializing views
            mEditText = (EditText) findViewById(R.id.editText);
            mTextView = (TextView) findViewById(R.id.constraint_textview);

           mEditText.addTextChangedListener(mTextEditorWatcher);

        }        
    }

更多说明:

该解决方案基于提供的信息 here关于计数器在屏幕外运行和 here关于如何使用TextWatcher

也请参见TextWatcherTextEdit上的android文档。

我用maxLength限制了TextEdit字段中输入字符的最大数量。 如果确实需要限制最大行数,请参阅讨论和可能的解决方案 herehere或在stackoverflow上使用搜索。