Angepasster unterer Blattdialog mit Kotlin

Dec 31 2022
Hallo zusammen, in diesem Artikel werde ich darüber sprechen, wie man seinen eigenen Dialog erstellt und wie man ihn benutzt. Um einen unteren Blattdialog in Kotlin zu implementieren, können Sie die BottomSheetDialog-Klasse aus der Android Support Library verwenden. In diesem Tutorial zeige ich Ihnen jedoch, wie Sie ein benutzerdefiniertes unteres Blatt mit einer Callback-Funktion erstellen.

Hallo zusammen, in diesem Artikel werde ich darüber sprechen, wie man seinen eigenen Dialog erstellt und wie man ihn benutzt.

Um einen unteren Blattdialog in Kotlin zu implementieren, können Sie die BottomSheetDialog-Klasse aus der Android Support Library verwenden. In diesem Tutorial zeige ich Ihnen jedoch, wie Sie ein benutzerdefiniertes unteres Blatt mit einer Callback-Funktion erstellen.

Schritt 1 : Wir erstellen eine abstrakte Klasse, die BottomSheetDialogFragment() erweitert.


abstract class RoundedBottomSheetDialogFragment : BottomSheetDialogFragment() {
    override fun getTheme(): Int = R.style.BaseBottomSheetDialog

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog = BottomSheetDialog(requireContext(), theme)

}

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>


    </data>



    <LinearLayout
        android:id="@+id/filter_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingStart="3dp"
        android:paddingEnd="3dp">


        <androidx.constraintlayout.widget.ConstraintLayout
            android:clickable="true"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:background="@drawable/bg_bottom_sheet_dialog_fragment"
            android:orientation="vertical"
            android:paddingTop="14dp"
            android:focusable="true">

            <View
                android:id="@+id/view_header_line"
                android:layout_width="56dp"
                android:layout_height="3dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="20dp"
                android:background="@drawable/corner_line" />



            <LinearLayout
                android:layout_marginTop="5dp"
                app:layout_constraintTop_toBottomOf="@+id/view_header_line"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/btn_fr"
                   android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="12dp"
                    android:padding="10dp"
                    android:paddingStart="40dp"
                    android:paddingEnd="40dp"
                    android:stateListAnimator="@null"
                    android:inputType="number"
                    android:hint="@string/label_btn_fr"
                    android:textColor="@color/black"
                    android:textSize="16sp"
                    app:backgroundTint="@color/white"
                    tools:targetApi="lollipop" />

                <com.google.android.material.button.MaterialButton
                    android:id="@+id/btn_confirmer"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_margin="2dp"
                    android:paddingStart="40dp"
                    android:paddingEnd="40dp"
                    android:padding="10dp"
                    android:stateListAnimator="@null"
                    android:text="@string/confirmer"
                    android:textColor="@color/black"
                    android:textSize="16sp"
                    app:backgroundTint="@color/white"
                    tools:targetApi="lollipop" />

            </LinearLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </LinearLayout>
</layout>

In der Fragmentklasse werden wir RoundedBottomSheetDialogFragment() erweitern und eine benutzerdefinierte Schnittstelle schreiben, um Klickereignisse abzufangen.


    private var onClickListener: OnClickListener? = null

    interface OnClickListener {
        fun onClickListener(textbottom: String)

    }

    fun setOnClickListener(onClickListener: OnClickListener) {
        this.onClickListener = onClickListener
    }


        mBinding.btnConfirmer.setOnClickListener {
            onClickListener?.onClickListener(mBinding.textFr.text.toString())
            dismiss()
        }


    fun show(fragmentManager: FragmentManager) {
        super.show(fragmentManager, TAG)
    }

companion object {

        val TAG = ModifierBottomSheet::class.java.simpleName
        fun newInstance(): ModifierBottomSheet {
            val fragment = ModifierBottomSheet()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }

    }

Wir erstellen eine neue Instanz des unteren Blatts

val mBottomSheetDialog =
                ModifierBottomSheet.newInstance()

mBottomSheetDialog.setOnClickListener(object :
                ModifierBottomSheet.OnClickListener {
            override fun onClickListener(textbottom: String) {
                    binding.baseText.text = textbottom
            }
        })

mBottomSheetDialog.show(this.supportFragmentManager)

private fun modifierRateFragment() {
        val mBottomSheetDialog =
                ModifierBottomSheet.newInstance()

        mBottomSheetDialog.setOnClickListener(object :
                ModifierBottomSheet.OnClickListener {
            override fun onClickListener(textbottom: String) {
                    binding.baseText.text = textbottom
            }
        })
        mBottomSheetDialog.show(this.supportFragmentManager)
    }

binding.baseButton.setOnClickListener {
            modifierRateFragment()
        }

Danke fürs Lesen, hoffe es hilft.