আপনি View-ভিত্তিক লেআউট ব্যবহার করলে, টগল প্রয়োগ করার জন্য
তিনটি প্রধান বিকল্প আছে। আমরা Material
Components লাইব্রেরি থেকে
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
দেখানো হয়েছে, যা হল অন্য একটি লিগ্যাসি কম্পোনেন্ট এবং এর UI উল্লেখযোগ্যভাবে আলাদা:
<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. } } } }
জাভা
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 বুলিয়ানের ভ্যালু
নতুন চেক করা অবস্থা নির্দেশ করে।