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

วิธีใช้เมธอด equals() ใน TextView ของ Android

ภาพรวม

ตัวอย่างนี้สาธิตการใช้เมธอด equals() เพื่อเปรียบเทียบสตริงสองค่าแบบ Case-sensitive (แยกตัวพิมพ์เล็ก-ใหญ่) ซึ่งเป็นวิธีมาตรฐานในการตรวจสอบความเท่ากันของ String ใน Java

ขั้นตอนการสร้างโปรเจกต์

ขั้นตอนที่ 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/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="ป้อนข้อความ" />

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

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp" />

</LinearLayout>

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

package com.example.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText name;
    Button button;
    TextView text;

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

        name = findViewById(R.id.name);
        button = findViewById(R.id.click);
        text = findViewById(R.id.textview);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = name.getText().toString().trim();
                
                if (input.isEmpty()) {
                    name.setError("กรุณาป้อนข้อความ");
                    return;
                }

                // เปรียบเทียบแบบ Case-sensitive
                boolean isMatch = input.equals("point");
                text.setText("ตรงกับ 'point' (Case-sensitive): " + isMatch);
            }
        });
    }
}

หลักการสำคัญของ equals()

  • Case-sensitive: "Point" และ "point" ไม่เท่ากัน
  • Null-safe: "point".equals(input) ปลอดภัยกว่า input.equals("point") หาก input อาจเป็น null
  • Symmetry: a.equals(b) ต้องเท่ากับ b.equals(a)
  • Override: คลาสที่สร้างเองควร Override equals() และ hashCode() คู่กัน

ตัวอย่างผลลัพธ์

ข้อความที่ป้อนequals("point")คำอธิบาย
"point"trueตรงกันทุกตัวอักษร
"POINT"falseตัวพิมพ์ใหญ่ต่างกัน
"Point"falseตัวแรกพิมพ์ใหญ่
" point "falseมีช่องว่างนำหน้า/ท้าย
"tutorialspoint"falseสตริงยาวกว่า

แนวทางปฏิบัติที่ดี (Best Practices)

// ✅ ดี - ปลอดภัยจาก null
if ("expected".equals(userInput)) { ... }

// ❌ รีดใช้ - อาจโดน NullPointerException
if (userInput.equals("expected")) { ... }

// ✅ ใช้ Objects.equals() สำหรับทั้งคู่อาจเป็น null
if (Objects.equals(str1, str2)) { ... }

กรณีการใช้งานจริง

  • ตรวจสอบรหัสผ่าน (Password verification)
  • เปรียบเทียบ Token/Key ที่ต้องแม่นยำทุกตัวอักษร
  • Validation ข้อมูลฟอร์มที่ Case-sensitive
  • การจับคู่ค่าคงที่ (Constants) เช่น Enum values