إذا كنت تستخدم تنسيقًا مستندًا إلى View
، هناك ثلاثة خيارات رئيسية
تنفيذ مفاتيح التبديل. ننصح باستخدام
المكوِّن SwitchMaterial
من Material
مكتبة المكونات:
<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
كلها فئات فرعية
عن CompoundButton
، والذي
آلية مشتركة للتعامل مع تغيرات الحالة المحددة. لقد نفذت
مثيل على
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. يُطلق على اللامدا
عندما تتغير حالة العلامة المحددة، وتتغير قيمة القيمة المنطقية isChecked
الذي يتم تمريره إلى lambda يشير إلى حالة التحديد الجديدة.