คุณสามารถนับอักขระจาก xml เองโดยใช้TextInputLayout wrapper สำหรับ EditText ที่แนะนำในSupportLibrary v23.1
เพียงห่อ EditText ของคุณด้วย TextInputLayout และตั้งค่า 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>
คุณจะได้รับเอฟเฟกต์วัสดุเช่น นี้
คุณอาจจะใช้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>
TextInputLayout TextInputEditText