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

相关内容

热门资讯

关于!微信微乐自建房辅助工具,... 关于!微信微乐自建房辅助工具,微乐家乡麻将自建房辅助app(开挂自建房)方法1)微乐家乡麻将自建房辅...
大家学习交流!微信卡五星辅助器... 大家学习交流!微信卡五星辅助器,微乐小程序免费黑科技(开挂自建房)方法1、很好的工具软件,可以解锁游...
三分钟了解!微乐微信自建房辅助... 三分钟了解!微乐微信自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)工具1、三分钟了解...
一分钟教会你!微乐家乡麻将自建... 一分钟教会你!微乐家乡麻将自建房辅助app,微乐自建房脚本免费入口(开挂自建房)方法1、用户打开应用...
分享实测!微信微乐自建房辅助工... 分享实测!微信微乐自建房辅助工具,微乐自建房脚本免费入口(开挂自建房)器1、起透看视 微信微乐自建房...
重大通报!微乐自建房辅助工具脚... 重大通报!微乐自建房辅助工具脚本,微乐小程序免费黑科技(开挂自建房)app微乐自建房辅助工具脚本破解...
如何分辨真伪!微乐微信自建房辅... 如何分辨真伪!微乐微信自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)技巧1、微乐微信自建房辅...
我来教教你!微乐自建房辅助工具... 我来教教你!微乐自建房辅助工具挂,微乐家乡麻将自建房辅助app(开挂自建房)工具1、玩家可以在微乐家...
重大来袭!微乐自建房辅助工具神... 重大来袭!微乐自建房辅助工具神器,微乐小程序免费黑科技(开挂自建房)器1、游戏颠覆性的策略玩法,独创...
带你了解!微信卡五星辅助器,微... 带你了解!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)方法1、带你了解!微信卡五星辅助器,...