Android Studio中使用Kotlin编写的AlertDialogue,其中的Spinner被设置为使用setItems进行填充。
创始人
2024-08-18 15:00:38
0

以下是一个在Android Studio中使用Kotlin编写的AlertDialogue,其中的Spinner被设置为使用setItems进行填充的示例代码:

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.Spinner
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val button: Button = findViewById(R.id.button)
        button.setOnClickListener {
            showAlertDialog()
        }
    }

    private fun showAlertDialog() {
        val builder = AlertDialog.Builder(this)
        val inflater = layoutInflater
        val dialogLayout: View = inflater.inflate(R.layout.dialog_layout, null)
        builder.setView(dialogLayout)

        val spinner: Spinner = dialogLayout.findViewById(R.id.spinner)
        val items = arrayOf("Item 1", "Item 2", "Item 3", "Item 4")
        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, items)
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spinner.adapter = adapter

        spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
                val selectedItem = parent.getItemAtPosition(position).toString()
                // 在此处执行选项被选择时的操作
            }

            override fun onNothingSelected(parent: AdapterView<*>) {
                // 在此处执行未选择选项时的操作
            }
        }

        builder.setPositiveButton("OK") { dialog, which ->
            // 在此处执行点击对话框中的"OK"按钮时的操作
        }
        builder.setNegativeButton("Cancel") { dialog, which ->
            // 在此处执行点击对话框中的"Cancel"按钮时的操作
        }

        builder.show()
    }
}

此示例中,我们首先在布局文件中创建了一个按钮(button)用于触发AlertDialogue的显示。当点击按钮时,调用showAlertDialog()方法显示AlertDialogue。

在showAlertDialog()方法中,我们首先创建了一个AlertDialog.Builder对象,并通过layoutInflater获取到dialog_layout布局文件的View对象。然后,我们找到该布局中的Spinner组件,并为其设置了适配器。

适配器使用了android.R.layout.simple_spinner_item布局文件作为Spinner的每个选项的布局,并使用了android.R.layout.simple_spinner_dropdown_item布局文件作为下拉菜单的选项布局。

接下来,我们为Spinner设置了一个OnItemSelectedListener,以便在用户选择选项时执行相应的操作。

最后,我们设置了对话框的"OK"和"Cancel"按钮的点击监听器,并调用builder.show()方法显示AlertDialogue。

请注意,你需要在res/layout文件夹中创建一个名为dialog_layout.xml的布局文件,并添加一个Spinner组件,其id设置为"spinner"。

相关内容

热门资讯

第三分钟辅助!微乐小程序辅助器... 第三分钟辅助!微乐小程序辅助器出租,微乐小程序免费黑科技,教材教程(有挂安装)-哔哩哔哩一、微乐小程...
第3分钟脚本!微乐广西小程序,... 第3分钟脚本!微乐广西小程序,微乐小程序免费黑科技,项目教程(有挂辅助)-哔哩哔哩微乐小程序免费黑科...
八分钟安装!微信微乐小程序辅助... 八分钟安装!微信微乐小程序辅助软件,微乐小程序免费黑科技,技法教程(有挂app)-哔哩哔哩微信微乐小...
第九分钟安装!微乐小游戏万能修... 第九分钟安装!微乐小游戏万能修改器,微乐小程序免费黑科技,举措教程(有挂工具)-哔哩哔哩1、微乐小游...
六分钟辅助!小程序微乐辅助器i... 六分钟辅助!小程序微乐辅助器ios,微乐小程序黑科技,举措教程(有挂修改器)-哔哩哔哩小程序微乐辅助...
第7分钟插件!微信小程序微乐辅... 第7分钟插件!微信小程序微乐辅助器贴吧,微乐小程序黑科技,法子教程(有挂脚本)-哔哩哔哩1、游戏颠覆...
9分钟脚本!微信小程序微乐挂如... 9分钟脚本!微信小程序微乐挂如何获取,微乐小程序黑科技,指南书教程(有挂软件)-哔哩哔哩1、9分钟脚...
第九分钟脚本!如何下载微乐自建... 第九分钟脚本!如何下载微乐自建房黑科技入口,微乐小程序免费黑科技,妙招教程(有挂平台)-哔哩哔哩1、...
七分钟脚本!微信小程序微乐辅助... 七分钟脚本!微信小程序微乐辅助,微乐小程序免费黑科技,窍门教程(有挂软件)-哔哩哔哩小薇(辅助器软件...
第3分钟辅助!微乐小程序破解,... 第3分钟辅助!微乐小程序破解,微乐小程序免费黑科技,模块教程(有挂神器)-哔哩哔哩所有人都在同一条线...