Android:AlertDialog自定义对话框

Dialog

    //加载自定义的view
    val view = LayoutInflater.from(this).inflate(R.layout.your_layout, null)
    val editText = view.findViewById<EditText>(R.id.et_dialog)
    
    //创建对话框
    AlertDialog.Builder(this).apply {
        setTitle("新建列表")  //设置标题
        setView(view)  //自定义你的view
        setNegativeButton("Cancel") { p0, p1 ->
        }
        setPositiveButton("创建列表") { p0, p1 ->
            val title = editText?.text.toString()
            if (title.isEmpty()) {
                Toast.makeText(this@YourActivity, "列表标题不能为空!", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this@YourActivity, "已添加列表: $title", Toast.LENGTH_SHORT).show()
            }
        }
        setCancelable(true)  //设置是否可以按返回键取消对话框
        show()  //显示此Dialog
    }   

your_layout布局XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:padding="10dp"
            android:src="@mipmap/ic_launcher"/>

        <EditText
            android:id="@+id/et_dialog"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="输入列表标题"
            android:textSize="20sp"/>

    </LinearLayout>

</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leekey_sjtu

我们一起从小白开始加油~~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值