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