Android AlertDialog setView,kotlin

272 篇文章 0 订阅

Android AlertDialog setView,kotlin

 

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.textfield.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/text"
        android:textSize="50sp"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:hint="please input..." />

</com.google.android.material.textfield.TextInputLayout>

 

 

import android.content.DialogInterface
import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val alertDialog: AlertDialog = AlertDialog.Builder(this).create()
        alertDialog.setTitle("my title")

        val view = LayoutInflater.from(this).inflate(R.layout.input_layout, null, false)
        alertDialog.setView(view)

        alertDialog.setButton(
            DialogInterface.BUTTON_POSITIVE,
            "ok",
            fun(dialog: DialogInterface, which: Int) {
                dialog.dismiss()
            }
        )

        alertDialog.setButton(
            DialogInterface.BUTTON_NEGATIVE,
            "cancel",
            fun(dialog: DialogInterface, which: Int) {
                dialog.dismiss()
            }
        )

        alertDialog.show()
    }
}

 

 

 

Android DialogFragment(1)_zhangphil的博客-CSDN博客Android DialogFragment(1)和过去的AlertDialog类似,Android引入的DialogFragment旨在为开发者提供一个“富”dialog,而不必受到过去Android AlertDialog的局限。首先,DialogFragment是一个Fragment,它有Fragment的一切属性和生命周期,其次,DialogFragment具有和AlertDihttps://blog.csdn.net/zhangphil/article/details/50886077

Android DialogFragment(2)_zhangphil的博客-CSDN博客Android DialogFragment(2)附录文章1简单介绍了如何实现一个DialogFragment,本文再介绍一种简单的方法:直接重写DialogFragment的onCreateDialog返回一个AlertDialog实现对话框。本文的例子和附录文章1不同的地方:不在依赖onCreateView。代码运行逻辑简述:功能简单,当点击FloatingActionButthttps://blog.csdn.net/zhangphil/article/details/50923828

Android Activity调整改变成Dialog_activity改成dialog_zhangphil的博客-CSDN博客做一个style配置到style.xml,直接作为该Activity的android:theme配置即可。具体的style: &lt;style name="DialogActivity" parent="@style/Theme.AppCompat.Dialog"&gt; &lt;item name="android:windowFullscreen"&gt;true&lt...https://blog.csdn.net/zhangphil/article/details/80258149

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Kotlin 和 XML 布局编写 AlertDialog 的示例代码: 1. 在 res/layout/ 目录下创建一个名为 custom_dialog.xml 的布局文件,用于定义 AlertDialog 的视图: ```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="wrap_content" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/dialog_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Dialog Title" android:textColor="@color/colorPrimary" android:textSize="18sp" android:textStyle="bold" /> <EditText android:id="@+id/dialog_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:hint="Enter text..." /> </LinearLayout> ``` 2. 在 Kotlin 代码中创建 AlertDialog,并将上面的 custom_dialog.xml 布局文件作为其视图: ```kotlin val builder = AlertDialog.Builder(this) val inflater = layoutInflater val dialogView = inflater.inflate(R.layout.custom_dialog, null) builder.setView(dialogView) val titleTextView = dialogView.findViewById<TextView>(R.id.dialog_title) titleTextView.text = "Custom Dialog Title" val inputEditText = dialogView.findViewById<EditText>(R.id.dialog_input) inputEditText.hint = "Enter text here..." builder.setPositiveButton("OK") { dialog, which -> val inputText = inputEditText.text.toString() // 执行 OK 操作 } builder.setNegativeButton("Cancel") { dialog, which -> dialog.dismiss() } val dialog = builder.create() dialog.show() ``` 在上面的代码中,我们首先使用 AlertDialog.Builder 创建一个 AlertDialog 对象。然后,我们使用 LayoutInflater 从 custom_dialog.xml 布局文件中创建一个视图,并将其设置为 AlertDialog 的视图。接下来,我们可以通过 findViewById() 方法获取布局文件中的视图,并对其进行操作。最后,我们设置 AlertDialog 的按钮操作,并显示它。 希望这可以帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值