1

我怎样才能做到这一点?我的意思是显示字符的限制以及在输入文本中数字化了多少。

EditText 字符界面

4

4 回答 4

8

您可以TextInputLayout在 xml 中简单地使用小部件来完成此任务,

<android.support.design.widget.TextInputLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:counterEnabled="true"
    app:counterMaxLength="50">

       <EditText
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:maxLength="50" />

</android.support.design.widget.TextInputLayout>

这里counterEnabled是启用计数器,并counterMaxLength设置计数器的最大范围。

在设置最大字符数maxLengthEditText,可以在您的 EditText 中输入。

于 2018-01-02T04:22:31.817 回答
2

您可以使用支持字符计数功能的TextInputLayout 。在这里,我已经提到了如何使用TextInputEditText实现TextInputLayout的完整描述

字符计数器

字符计数器是很多应用程序使用的功能。(还记得 Twitter 字符限制吗?)。将app :counterEnabled 设置为true ,并将app:counterMaxLength设置为 TextInputLayout 中所需的最大字符数。字符计数器默认显示在 EditText 下方(右下​​角),在编写本教程时,还无法更改位置。设置计数器的样式类似于设置提示文本的样式。app:counterTextAppearance是这次使用的属性。我们在项目的 styles.xml 文件中添加了以下样式。

<style name="CounterText" parent="TextAppearance.Design.Counter">
        <item name="android:textSize">16sp</item>
        <item name="android:textColor">@color/my_pink</item>
    </style>

下面的 xml 代码来自activity_main.xml布局,并具有带有默认字符计数器和自定义字符计数器的 EditText 字段。

 <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/activity_horizontal_margin"
            app:counterEnabled="true"
            app:counterMaxLength="5"
            app:hintTextAppearance="@style/HintText">

            <android.support.design.widget.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Character Counter Limit 10" />

        </android.support.design.widget.TextInputLayout>

上述代码的输出如下所示。

在此处输入图像描述

于 2018-01-02T05:04:53.987 回答
1

你可以用 TextInputLayout 做到这一点:

app:counterEnabled="true"
app:counterMaxLength="250"

例子

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true"
app:counterMaxLength="250">

   <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxLength="250" />

</android.support.design.widget.TextInputLayout>
于 2018-01-02T03:55:10.850 回答
1

实现它看起来像这样:

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

        public void onTextChanged(CharSequence s, int start, int before, int count) {
           //This sets a textview to the current length
           mTextView.setText(String.valueOf(s.length()));
        }

        public void afterTextChanged(Editable s) {
        }
};

然后您必须将编辑文本设置为侦听器:

mEditText.addTextChangedListener(mTextEditorWatcher);

然后在布局中将您的编辑文本包装在以下 TextInputLayout 中:

<android.support.design.widget.TextInputLayout
    android:id="@+id/textContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:counterEnabled="true"
    app:counterMaxLength="20"
    >
    <EditText
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Text Hint"
        />
</android.support.design.widget.TextInputLayout>

祝你好运 :)

于 2018-01-02T03:57:57.473 回答