วิธีนับตัวอักษรใน EditText ด้วย TextWatcher ใน Android
บางสถานการณ์ เราต้องจำกัด EditText ให้รองรับตัวอักษรบางจำนวน เพื่อแก้ไขสถานการณ์นี้ ตัวอย่างนี้สาธิตวิธีนับตัวอักษรใน EditText Changed Listener
ขั้นตอนที่ 1 - สร้างโปรเจกต์ใหม่ใน Android Studio
ไปที่ File → New Project และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโปรเจกต์ใหม่
ขั้นตอนที่ 2 - เพิ่มโค้ดใน res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/parent"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
android:background="#33FFFF00"
android:orientation="vertical">
<EditText
android:id="@+id/text"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
ในโค้ดด้านบน เราใช้ EditText หนึ่งตัว จะตรวจสอบความยาวของตัวอักษรที่ป้อน หากเกิน 5 ตัวอักษร จะแสดงข้อความผิดพลาด
ขั้นตอนที่ 3 - เพิ่มโค้ดใน src/MainActivity.java
package com.example.andy.myapplication;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.support.annotation.RequiresApi;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int view = R.layout.activity_main;
EditText text;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(view);
text = findViewById(R.id.text);
text.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) {
if (s.toString().length() > 5) {
text.setError("It allows only 5 character");
} else {
text.setError(null);
}
}
});
}
}
ในโค้ดด้านบน เราใช้ Text Changed Listener หลังจากเปลี่ยนข้อความเราตรวจสอบข้อความดังนี้:
text.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) {
if (s.toString().length() > 5) {
text.setError("It allows only 5 character");
} else {
text.setError(null);
}
}
});
ลองรันแอปพลิเคชันของคุณ สมมติว่าคุณเชื่อมต่ออุปกรณ์ Android จริงกับคอมพิวเตอร์แล้ว เพื่อรันแอปจาก Android Studio ให้เปิดไฟล์กิจกรรมของโปรเจกต์หนึ่งและคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก และตรวจสอบอุปกรณ์ซึ่งจะแสดงหน้าจอเริ่มต้น

เมื่อคุณป้อน 5 ตัวอักษร ไม่แสดงข้อผิดพลาด หากป้อนมากกว่า 5 ตัวอักษร จะแสดงข้อผิดพลาดดังภาพด้านล่าง
