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... 科普攻略!德普之星辅助器app,we poker辅助器,德州论坛(有挂软件)是一款可以让一直输的玩家...
重大科普!佛手在线大菠萝智能辅... 重大科普!佛手在线大菠萝智能辅助器,wepoker作弊辅助,分享教程(有挂软件);原来确实真的有挂(...
一分钟教会你!wepoker怎... 一分钟教会你!wepoker怎么增加运气,epoker透视,切实教程(有挂透视)1、点击下载安装,微...
六分钟了解!hhpoker有辅... 六分钟了解!hhpoker有辅助吗,wepoker国外版透视,扑克教程(有挂技巧)科技教程也叫必备教...
我来教大家!wepoker辅助... 我来教大家!wepoker辅助透视,wepoker免费脚本弱密码,详细教程(有挂透明);wepoke...
记者发布!wpk辅助,德普之星... 记者发布!wpk辅助,德普之星透视辅助软件激活码,解密教程(有挂辅助);亲真的是有正版授权,小编(透...
揭秘攻略!aapoker万能辅... 《揭秘攻略!aapoker万能辅助器,hhpoker真的假的,揭秘教程(有挂教程)》 aapoker...
重大通报!sohoo poke... 自定义sohoo poker辅助器系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用...
三分钟了解!wpk辅助器,hh... 1、三分钟了解!wpk辅助器,hhpoker免费辅助器,必赢教程(有挂神器);详细教程。2、hhpo...
玩家必看攻略!wejoker私... 玩家必看攻略!wejoker私人辅助软件,智星德州可以透视吗,透明挂教程(有挂技巧)关于智星德州可以...