เพิ่มปุ่มเปิด/ปิด

ลองใช้วิธีแบบ Compose
Jetpack Compose เป็นชุดเครื่องมือ UI ที่แนะนำสำหรับ Android ดูวิธีเพิ่มคอมโพเนนต์ใน Compose

หากคุณใช้เลย์เอาต์ที่อิงตาม View คุณจะมีตัวเลือกหลัก 3 ตัวเลือกในการใช้ปุ่มเปิด/ปิด เราขอแนะนำให้ใช้คอมโพเนนต์ SwitchMaterial จากไลบรารี Material Components

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <com.google.android.material.switchmaterial.SwitchMaterial
        android:id="@+id/material_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/material_switch"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

แอปเวอร์ชันเดิมอาจยังคงใช้คอมโพเนนต์ AppCompat เวอร์ชันเก่า ดังที่แสดงในตัวอย่างต่อไปนี้SwitchCompat

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <androidx.appcompat.widget.SwitchCompat
        android:id="@+id/switchcompat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/switchcompat"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

ตัวอย่างต่อไปนี้แสดง AppCompatToggleButton, ซึ่งเป็นคอมโพเนนต์เวอร์ชันเดิมอีกรายการหนึ่งที่มี UI แตกต่างกันอย่างเห็นได้ชัด

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <TextView
        android:id="@+id/toggle_button_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/toggle"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintBaseline_toBaselineOf="@id/toggle"
        android:text="@string/toggle_button" />

    <androidx.appcompat.widget.AppCompatToggleButton
        android:id="@+id/toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/toggle_button_label"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

คอมโพเนนต์ทั้ง 3 รายการนี้มีลักษณะการทำงานเหมือนกัน แต่มีลักษณะที่แตกต่างกัน SwitchMaterial และ SwitchCompat มีความแตกต่างกันเล็กน้อย แต่ AppCompatToggleButton แตกต่างกันอย่างเห็นได้ชัด

SwitchMaterial, SwitchCompat และ AppCompatToggleButton
Controls

รูปที่ 1 ปุ่มเปิด/ปิด 3 ประเภท

จัดการการเปลี่ยนแปลงสถานะ

SwitchMaterial, SwitchCompat, และ AppCompatToggleButton เป็นคลาสย่อย ของ CompoundButton ซึ่ง ทำให้คอมโพเนนต์เหล่านี้มีกลไกทั่วไปสำหรับการจัดการการเปลี่ยนแปลงสถานะที่เลือก คุณใช้ an instance of CompoundButton.OnCheckedChangeListener และเพิ่มลงในปุ่ม ดังที่แสดงในตัวอย่างต่อไปนี้

Kotlin

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding: SwitchLayoutBinding = SwitchLayoutBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.materialSwitch.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                // The switch is checked.
            } else {
                // The switch isn't checked.
            }
        }
    }
}

Java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SwitchLayoutBinding binding = SwitchLayoutBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.materialSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                // The switch is checked.
            } else {
                // The switch isn't checked.
            }
        });
    }
}

CompoundButton.OnCheckedChangeListener เป็นอินเทอร์เฟซเมธอดแอบสแตรกเดี่ยว (หรือ อินเทอร์เฟซ SAM) คุณจึงใช้เป็น lambda ได้ ระบบจะเรียก lambda ทุกครั้งที่สถานะที่เลือกมีการเปลี่ยนแปลง และค่าบูลีน isChecked ที่ส่งไปยัง lambda จะระบุสถานะที่เลือกใหม่