EditText的实时字符数

Tay*_*ins 91 android

我想知道在Android中编辑文本框的实时字符数的最佳方法是什么.我在看这个,但我似乎无法理解它.

为了描述这个问题,我有一个EditText,我试图将字符限制为150.我可以使用输入过滤器来执行此操作,但是我想在文本框的正下方显示用户输入的字符数(几乎就像堆栈溢出现在正在做的那样).

如果有人可以写一小段示例代码或指向正确的方向,我会非常感激.

Cam*_*ham 148

您可以使用TextWatcher查看文本何时更改

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) {
        }
};
Run Code Online (Sandbox Code Playgroud)

使用设置edittext的TextWatcher

mEditText.addTextChangedListener(mTextEditorWatcher);
Run Code Online (Sandbox Code Playgroud)

  • 试过这个,效果很好!应该选择正确答案! (5认同)
  • 它通过替换为mTextView.setText(String.valueOf(150-s.length()))来解决; 代替mTextView.setText(String.valueOf(s.length())); (5认同)
  • 添加计数的最佳方法是在文本框的右下角.扩展编辑文本并手动绘制字符串? (2认同)

Mid*_*mar 101

您可以使用SupportLibrary v23.1中引入的EditText的TextInputLayout包装器从xml本身进行字符计数

只需使用TextInputLayout包装EditText并将CounterEnabled设置为true并设置counterMaxLength.

<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>
Run Code Online (Sandbox Code Playgroud)

你会得到这样一个重大的影响

您可以使用counterOverflowTextAppearance,counterTextAppearance来设置计数器的样式.

编辑

来自Android文档.

TextInputEditText提供一流用作此布局的孩子.使用TextInputEditText允许TextInputLayout更好地控制任何文本输入的可视方面.示例用法如下:

     <android.support.design.widget.TextInputLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content">

     <android.support.design.widget.TextInputEditText
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:hint="@string/form_username"/>

 </android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

TextInputLayout TextInputEditText

  • 这正是我正在寻找的:)一个干净的,支持库impl.谢谢 (3认同)
  • 应该是接受的答案!我完全错过了那些属性! (3认同)

Dan*_*ico 21

你可以使用TextInputLayout和compat库来实现:

app:counterEnabled="true"
app:counterMaxLength="420"
Run Code Online (Sandbox Code Playgroud)

并完成:

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

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

</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)


Ama*_*onz 11

在xml中为editText添加此属性

    android:maxLength="80"
Run Code Online (Sandbox Code Playgroud)

在java中添加此侦听器

  ed_caption.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            tv_counter.setText(80 - s.toString().length() + "/80");

        }
    });
Run Code Online (Sandbox Code Playgroud)


小智 6

非常简单按照以下说明操作:

====将它们添加到您的Imports ===

import android.text.Editable;
import android.text.TextWatcher;
Run Code Online (Sandbox Code Playgroud)

=====定义这个=====

private TextView sms_count;
Run Code Online (Sandbox Code Playgroud)

========== Inside On Create =====

sms_count = (TextView) findViewById(R.id.textView2);


final TextWatcher txwatcher = new TextWatcher() {
   public void beforeTextChanged(CharSequence s, int start, int count, int after) {
   }

   public void onTextChanged(CharSequence s, int start, int before, int count) {

      sms_count.setText(String.valueOf(s.length()));
   }

   public void afterTextChanged(Editable s) {
   }
};

sms_message.addTextChangedListener(txwatcher);
Run Code Online (Sandbox Code Playgroud)


小智 5

    You can use TextWatcher class to see text has changed and how much number of character remains.Here i have set counter of 140 characters.

    EditText typeMessageToPost;
    TextView number_of_character;
public void onCreate(Bundle savedBundleInstance) {
        super.onCreate(savedBundleInstance);
setContentView(R.layout.post_activity);
typeMessageToPost.addTextChangedListener(mTextEditorWatcher);
}
private final TextWatcher mTextEditorWatcher=new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            number_of_character.setText(String.valueOf(140-s.length()));
        }
    };
Run Code Online (Sandbox Code Playgroud)


Sat*_*ish 5

您可以向包装在 TextInputLayout 中的 TextInputEditText 添加一个计数器。正如您在示例中看到的,counterEnabled启用此功能并counterMaxLengh为其定义字符数。

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/til_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:counterEnabled="true"
        app:counterMaxLength="50">
    <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/et_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)