kotlin代碼 寫一個alterdialog,還有一個textView
时间: 2023-06-19 20:07:36 浏览: 55
以下是使用 Kotlin 编写的 AlertDialog 和 TextView 示例代码:
```kotlin
// 引入需要的包
import android.app.AlertDialog
import android.content.Context
import android.widget.TextView
// 函数:显示包含 TextView 的 AlertDialog
fun showAlertDialogWithTextView(context: Context, message: String) {
// 创建 TextView
val textView = TextView(context)
textView.text = message
// 创建 AlertDialog
val builder = AlertDialog.Builder(context)
builder.setTitle("AlertDialog with TextView")
builder.setView(textView)
// 显示 AlertDialog
val alertDialog = builder.create()
alertDialog.show()
}
```
在你的代码中,你可以像这样调用 `showAlertDialogWithTextView` 函数:
```kotlin
showAlertDialogWithTextView(this, "Hello, World!")
```
这将会显示一个包含 "Hello, World!" 文字的 AlertDialog。