Kotlin-Android中的自定义对话框

Abd*_*yev 20 android kotlin

我想在Kotlin中创建自己的对话框。我在Stack Overflow上浏览了有关此主题的问题,但是找不到任何有用的信息。我该怎么做?

Sho*_*ana 22

您可以在下面的代码中使用自定义对话框。这是我的工作代码。

 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()

}
Run Code Online (Sandbox Code Playgroud)

  • @RichardBarraclough我相信它应该是上下文,所以可以使用`this` (2认同)

Jen*_*ter 7

在我的解决方案下面作为一种“消息框”。我还没有实现“确定”按钮。单击消息框后应关闭。

这里的布局元素(*/layout/message_box.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/message_box_header"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:textAlignment="center"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/message_box_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:textSize="20sp" />

    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我在 Fragment 类中实现了这个功能。它是用 Kotlin 编写的。

fun showMessageBox(text: String){

    //Inflate the dialog as custom view
    val messageBoxView = LayoutInflater.from(activity).inflate(R.layout.message_box, null)

    //AlertDialogBuilder
    val messageBoxBuilder = AlertDialog.Builder(activity).setView(messageBoxView)

    //setting text values
    messageBoxView.message_box_header.text = "This is message header"
    messageBoxView.message_box_content.text = "This is message content"

    //show dialog
    val  messageBoxInstance = messageBoxBuilder.show()

    //set Listener
    messageBoxView.setOnClickListener(){
        //close dialog
        messageBoxInstance.dismiss()
    }
}
Run Code Online (Sandbox Code Playgroud)


Rez*_*reh 6

您在 kotlin 中的 context 扩展函数上有一个干净的代码,并在您的所有代码中使用它

fun Context.showDialog(
    title: String,
    description: String,
    titleOfPositiveButton: String? = null,
    titleOfNegativeButton: String? = null,
    positiveButtonFunction: (() -> Unit)? = null,
    negativeButtonFunction: (() -> Unit)? = null
) {
    val dialog = Dialog(this, R.style.Theme_Dialog)
    dialog.window?.requestFeature(Window.FEATURE_NO_TITLE) // if you have blue line on top of your dialog, you need use this code
    dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    dialog.setCancelable(false)
    dialog.setContentView(R.layout.dialog_custom_layout)
    val dialogTitle = dialog.findViewById(R.id.title) as TextView
    val dialogDescription = dialog.findViewById(R.id.description) as TextView
    val dialogPositiveButton = dialog.findViewById(R.id.positiveButton) as TextView
    val dialogNegativeButton = dialog.findViewById(R.id.negativeButton) as TextView
    dialogTitle.text = title
    dialogDescription.text = description
    titleOfPositiveButton?.let { dialogPositiveButton.text = it } ?: dialogPositiveButton.makeGone()
    titleOfNegativeButton?.let { dialogNegativeButton.text = it } ?: dialogNegativeButton.makeGone()
    dialogPositiveButton.setOnClickListener {
        positiveButtonFunction?.invoke()
        dialog.dismiss()
    }
    dialogNegativeButton.setOnClickListener {
        negativeButtonFunction?.invoke()
        dialog.dismiss()
    }
    dialog.show()
}
Run Code Online (Sandbox Code Playgroud)

这是使用此功能的示例

requireContext().showDialog(
            title = "Your Title",
            description = "Your Description",
            titleOfPositiveButton = "yes",
            titleOfNegativeButton = "No",
            positiveButtonFunction = { // Run codes after click on positive button },
            negativeButtonFunction = { // Run codes after click on negative button }
        )
Run Code Online (Sandbox Code Playgroud)

你需要在对话框的设计上保持一致的风格

<style name="Theme_Dialog" parent="android:Theme.Holo.Dialog">
        <item name="android:windowMinWidthMajor">90%</item>
        <item name="android:windowMinWidthMinor">90%</item>
    </style>
Run Code Online (Sandbox Code Playgroud)


Eln*_*ech 5

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>
Run Code Online (Sandbox Code Playgroud)

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)

    }
}
Run Code Online (Sandbox Code Playgroud)