Kotlin - пользовательский диалог в Android - программирование
Подтвердить что ты не робот

Kotlin - пользовательский диалог в Android

Я хочу создать свой собственный диалог в Котлине. Я просмотрел вопросы по этой теме в Stack Overflow, но я не нашел никакой полезной информации. Как мне это сделать?

4b9b3361

Ответ 1

Вы можете использовать ниже код для пользовательского диалога. Это мой рабочий код.

 private fun showDialog(title: String) {
    val dialog = Dialog(activity)
    dialog .requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog .setCancelable(false)
    dialog .setContentView(R.layout.cutsom_layout)
    val body = dialog .findViewById(R.id.body) as TextView
    body.text = title
    val yesBtn = dialog .findViewById(R.id.yesBtn) as Button
    val noBtn = dialog .findViewById(R.id.noBtn) as TextView
    yesBtn.setOnClickListener {
        dialog .dismiss()
    }
    noBtn.setOnClickListener { dialog .dismiss() }
    dialog .show()

}

Ответ 2

Это способ, которым вы можете создать свой собственный диалог с пользовательским макетом.

val dialogBuilder = AlertDialog.Builder(context, R.style.AlertDialogTheme)
    val inflater = this.layoutInflater
    val dialogView = inflater.inflate(R.layout.layout_chat_type_selection, null)
    dialogBuilder.setView(dialogView)
    val radioGroupChat = dialogView.radio_group_chat
    dialogView.radioButton_user_chat.isChecked = true
    dialogBuilder.setPositiveButton(getString(R.string.ok_text), object : DialogInterface.OnClickListener {
        override fun onClick(dialog: DialogInterface, id: Int) {
            when (radioGroupChat.checkedRadioButtonId) {
                R.id.radioButton_user_chat -> {
                    (activity as HomeActivity).replaceFragment(MySkippersFragment.getInstance(isFromChat = true))
                }
                R.id.radioButton_circle_chat -> {
                    (activity as HomeActivity).replaceFragment(PickCircleFragment.getInstance(
                        PickCircleFragment.NEW_CIRCLE_CHAT), true)
                }
            }
        }
    })
    dialogBuilder.setNegativeButton(getString(R.string.cancel_text), object : DialogInterface.OnClickListener {
        override fun onClick(dialog: DialogInterface?, which: Int) {
        }
    })

    val alertDialog = dialogBuilder.create()
    alertDialog.show()

Ответ 3

custom_dialog.xml

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/fitsdk_white_rectangle"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="30dp"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="30dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/error_timeout_title"
            android:textColor="@color/black" />

        <TextView
            android:id="@+id/tvBody"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:text="@string/error_timeout_body"
            android:textColor="@color/black" />

        <Button
            android:id="@+id/btn_yes"
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:background="@android:color/white"
            android:clickable="true"
            android:text="Yes"
            android:textColor="#5DBCD2"
            android:textStyle="bold" />
    </LinearLayout>

CustomDialogClass.kt

   class CustomDialogClass(context: Context) : Dialog(context) {

    init {
        setCancelable(false)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.custom_dialog)

    }
}