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