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文件


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

你可能感兴趣的:(Android,android,kotlin,android,studio)