افزودن دکمه‌های مبدل

امتحان کردن روش Compose
Jetpack Compose جعبه‌ابزار واسط کاربر توصیه‌شده برای Android است. با نحوه افزودن مؤلفه‌ها در Compose آشنا شوید.

اگر از چیدمان مبتنی بر View استفاده می‌کنید، سه انتخاب اصلی برای پیاده‌سازی کلیدهای روشن/خاموش وجود دارد. توصیه می‌کنیم از مؤلفه 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>

برنامه‌های قدیمی ممکن است همچنان از مؤلفه قدیمی‌تر SwitchCompat AppCompat استفاده کنند، همان‌طور که در مثال زیر نشان داده شده است:

<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 را نشان می‌دهد، که یکی دیگر از مؤلفه‌های قدیمی است که میانای کاربری به‌طور قابل‌توجهی متفاوت دارد:

<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>

این سه مؤلفه رفتار یکسانی دارند اما ظاهرشان متفاوت است. تفاوت‌های بین SwitchMaterial و SwitchCompat جزئی است، اما AppCompatToggleButton تفاوت قابل‌توجهی دارد:

کنترل‌های
SwitchMaterial،‏ SwitchCompat، و AppCompatToggleButton

شکل ۱. سه نوع دکمه مبدل.

رسیدگی به تغییرات وضعیت

‫SwitchMaterial،‏ SwitchCompat، و AppCompatToggleButton همگی زیرکلاس‌های CompoundButton هستند که سازوکار مشترکی برای رسیدگی به تغییرات وضعیت علامت‌زده‌شده به آن‌ها می‌دهد. نمونه‌ای از CompoundButton.OnCheckedChangeListener را پیاده‌سازی می‌کنید و آن را به دکمه اضافه می‌کنید، همان‌طور که در مثال زیر نشان داده شده است:

کاتلین

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.
            }
        }
    }
}

جاوا

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 فراخوانی می‌شود و مقدار بولی isChecked که به lambda ارسال می‌شود، وضعیت علامت‌گذاری‌شده جدید را نشان می‌دهد.