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

วิธีใช้ CheckBox ใน Android

ภาพรวม

ตัวอย่างนี้สาธิตการใช้ CheckBox สำหรับสร้างเมนูสั่งอาหาร เลือกรายการหลายอย่างพร้อมกัน และคำนวณราคารวมเมื่อกดปุ่มสั่งซื้อ

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

Android Studio → File → New Project → Empty Activity

ขั้นตอนที่ 2: จัดรูปแบบหน้าจอ (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    tools:context=".MainActivity">

    <CheckBox
        android:id="@+id/checkBoxPizza"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="144dp"
        android:layout_marginTop="68dp"
        android:text="Pizza - 100 บาท"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/checkBoxCoffee"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="144dp"
        android:layout_marginTop="28dp"
        android:text="Coffee - 50 บาท"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/checkBoxPizza" />

    <CheckBox
        android:id="@+id/checkBoxBurger"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="144dp"
        android:layout_marginTop="28dp"
        android:text="Burger - 120 บาท"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/checkBoxCoffee" />

    <Button
        android:id="@+id/buttonOrder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="144dp"
        android:layout_marginTop="184dp"
        android:text="สั่งซื้อ"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/checkBoxBurger" />

</androidx.constraintlayout.widget.ConstraintLayout>

ขั้นตอนที่ 3: เขียนโค้ด MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private CheckBox checkBoxPizza, checkBoxCoffee, checkBoxBurger;
    private Button buttonOrder;

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

        checkBoxPizza = findViewById(R.id.checkBoxPizza);
        checkBoxCoffee = findViewById(R.id.checkBoxCoffee);
        checkBoxBurger = findViewById(R.id.checkBoxBurger);
        buttonOrder = findViewById(R.id.buttonOrder);

        buttonOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int totalAmount = 0;
                StringBuilder result = new StringBuilder();
                result.append("รายการที่สั่ง:\n");

                if (checkBoxPizza.isChecked()) {
                    result.append("Pizza - 100 บาท\n");
                    totalAmount += 100;
                }
                if (checkBoxCoffee.isChecked()) {
                    result.append("Coffee - 50 บาท\n");
                    totalAmount += 50;
                }
                if (checkBoxBurger.isChecked()) {
                    result.append("Burger - 120 บาท\n");
                    totalAmount += 120;
                }

                result.append("\nรวมทั้งหมด: ").append(totalAmount).append(" บาท");
                Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();
            }
        });
    }
}

ขั้นตอนที่ 4: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

การทดสอบ

รันแอป → ติ๊ก CheckBox ที่ต้องการ → กดปุ่ม "สั่งซื้อ" → จะแสดง Toast สรุปรายการและราคารวม

วิธีใช้ CheckBox ใน Android วิธีใช้ CheckBox ใน Android

ขยายความสามารถ

  • ใช้ CompoundButton.OnCheckedChangeListener เพื่ออัปเดตราคา Real-time
  • เก็บข้อมูลเมนูใน Array/List แทน Hardcode เพื่อเพิ่ม/ลบรายการได้ง่าย
  • เชื่อมต่อกับฐานข้อมูลหรือ API สำหรับแอปจริง
  • ใช้ RecyclerView + CheckBox สำหรับรายการเยอะ