新增字元計數

TextInputLayout 具有在其中定義的 EditText 的字元計數器 。
計數器將呈現在 EditText 下方。

只需使用 setCounterEnabled() 和 setCounterMaxLength 方法:

TextInputLayout til = (TextInputLayout) findViewById(R.id.username);
til.setCounterEnabled(true);
til.setCounterMaxLength(15);

或者 xml 中的 app:counterEnabled 和 app:counterMaxLength 屬性。

<android.support.design.widget.TextInputLayout
    app:counterEnabled="true"
    app:counterMaxLength="15">

    <EditText/>

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