הוספת מתגים

רוצה לנסות את שיטת הכתיבה?
'Jetpack פיתוח נייטיב' היא ערכת הכלים המומלצת לממשק המשתמש ל-Android. מידע על הוספת רכיבים לכתיבה.

אם משתמשים בפריסה שמבוססת על View, יש 3 אפשרויות עיקריות הטמעה של מתגים. מומלץ להשתמש רכיב SwitchMaterial מתוך חומר הלימוד ספריית הרכיבים:

<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 ו-AppCompatToggle
פרמטרים להשוואה

איור 1. שלושה סוגים של לחצנים.

טיפול בשינויי מצב

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 מציין את המצב החדש המסומן.