Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีตรวจสอบตัวอักษรแรกของ EditText ว่าเป็นตัวอักษรหรือไม่

ภาพรวม

บทความนี้สาธิตวิธีตรวจสอบว่าตัวอักษรแรกใน EditText เป็นตัวอักษร (Alphabet A-Z, a-z) หรือไม่

ขั้นตอนที่ 1: สร้างโปรเจกต์

Android Studio → File → New Project → Empty Activity

ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edit_query"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="ป้อนข้อความ" />

    <Button
        android:id="@+id/buttonPanel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ตรวจสอบ" />

</LinearLayout>

ขั้นตอนที่ 3: โค้ดหลัก (java/MainActivity.java)

package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText editQuery = findViewById(R.id.edit_query);
        findViewById(R.id.buttonPanel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String data = editQuery.getText().toString().trim();

                if (data.isEmpty()) {
                    Toast.makeText(MainActivity.this, "กรุณาป้อนข้อความ", Toast.LENGTH_SHORT).show();
                    return;
                }

                char firstChar = data.charAt(0);

                // วิธีที่ 1: ใช้ Character.isLetter() (แนะนำ - รองรับ Unicode)
                boolean isAlphabet = Character.isLetter(firstChar);

                // วิธีที่ 2: เปรียบเทียบช่วง ASCII (เฉพาะ A-Z, a-z)
                // boolean isAlphabet = (firstChar >= 'a' && firstChar <= 'z') || (firstChar >= 'A' && firstChar <= 'Z');

                if (isAlphabet) {
                    Toast.makeText(MainActivity.this, firstChar + " เป็นตัวอักษร", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this, firstChar + " ไม่ใช่ตัวอักษร", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

เปรียบเทียบวิธีการตรวจสอบ

วิธีคำอธิบาย
Character.isLetter(ch)แนะนำที่สุด - ตรวจสอบทุกภาษา (ไทย, จีน, อรบิก, อังกฤษ, ฯลฯ)
ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'เฉพาะอังกฤษ ASCII - ไม่รองรับภาษาอื่น
Pattern.matches("[a-zA-Z]", String.valueOf(ch))ใช้ Regex - ยืดหยุ่นแต่ช้ากว่า

ทดสอบ

ป้อน "Hello" → "H เป็นตัวอักษร"
ป้อน "123" → "1 ไม่ใช่ตัวอักษร"
ป้อน "สวัสดี" → "ส เป็นตัวอักษร" (ถ้าใช้ isLetter)
ป้อน "@email" → "@ ไม่ใช่ตัวอักษร"