अगर View-आधारित लेआउट का इस्तेमाल किया जा रहा है, तो टॉगल लागू करने के लिए तीन मुख्य विकल्प हैं. हमारा सुझाव है कि आप मटीरियल कॉम्पोनेंट लाइब्रेरी में मौजूद 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, और 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 एक सिंगल ऐब्स्ट्रैक्ट मैथड इंटरफ़ेस (या एसएएम इंटरफ़ेस) है. इसलिए, इसे एक लैम्ब्डा फ़ंक्शन के तौर पर लागू किया जा सकता है. जब भी सही के निशान की स्थिति बदलती है, तब लैम्डा को कॉल किया जाता है. साथ ही, लैम्डा को पास की गई isChecked बूलियन वैल्यू से, जांच की गई नई स्थिति के बारे में पता चलता है.