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

วิธีใช้ compareTo() ใน TextView Android เปรียบเทียบข้อความ

ภาพรวม

บทความนี้สาธิตการใช้ String.compareTo() เปรียบเทียบข้อความใน EditText กับคำอ้างอิง

ขั้นตอนที่ 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"
    android:padding="16dp"
    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:layout_marginTop="16dp"
        android:text="เปรียบเทียบ" />

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

</LinearLayout>

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

package com.example.myapplication;

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

public class MainActivity extends AppCompatActivity {

    private EditText nameEditText;
    private Button compareButton;
    private TextView resultTextView;

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

        nameEditText = findViewById(R.id.name);
        compareButton = findViewById(R.id.click);
        resultTextView = findViewById(R.id.textview);

        compareButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = nameEditText.getText().toString().trim();

                if (input.isEmpty()) {
                    nameEditText.setError("กรุณาป้อนชื่อ");
                    return;
                }

                String reference = "sairam";
                int result = input.compareTo(reference);

                String message;
                if (result == 0) {
                    message = "เหมือนกันพอดี (0)";
                } else if (result < 0) {
                    message = "น้อยกว่า (Negative: " + result + ") - มาก่อนในลำดับ Dictionary";
                } else {
                    message = "มากกว่า (Positive: " + result + ") - มาหลังในลำดับ Dictionary";
                }

                resultTextView.setText("ผลลัพธ์: " + message);
            }
        });
    }
}

คำอธิบาย compareTo()

คืนค่าความหมาย
0สตริงทั้งสองเหมือนกันทุกตัวอักษร
< 0 (Negative)สตริงต้นทาง (this) น้อยกว่า argument - มาก่อนใน Dictionary order
> 0 (Positive)สตริงต้นทาง มากกว่า argument - มาหลังใน Dictionary order

ตัวอย่างค่า

"apple".compareTo("banana")   // -1 (a < b)
"banana".compareTo("apple")   // 1 (b > a)
"apple".compareTo("apple")    // 0
"apple".compareTo("app")      // 2 (length diff: 'l'=108, 'e'=101)
"ABC".compareTo("abc")        // -32 (ASCII: 'A'=65, 'a'=97)

Case-Insensitive Comparison

int result = input.compareToIgnoreCase("sairam");

Kotlin

val result = input.compareTo("sairam")
// หรือ
val result = input.compareTo("sairam", ignoreCase = true)

กรณีใช้งาน

  • Sorting リスト (Collections.sort ใช้ compareTo)
  • Validation: ตรวจสอบรหัสผ่าน, PIN
  • Search/Filter logic
  • Version comparison (semver)

ทดสอบ

ป้อน "sairam" → "เหมือนกันพอดี (0)"
ป้อน "aaa" → "น้อยกว่า (Negative)"
ป้อน "zzz" → "มากกว่า (Positive)"