Android:Spinner:在代码中设置 ArrayAdapter 不希望调用 onItemSelected,仅在用户单击 spinner 时调用

问题描述 投票:0回答:1

Android Spinner:当我在代码中设置ArrayAdapter时,android调用onItemSelected。仅当用户单击微调项目时才可以调用?

听众

binding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
        override fun onItemSelected(adapterView: AdapterView<*>?, view: View, i: Int, l: Long) {
            value = binding.spinner.selectedItem.toString()

            when (value) {
                'a' -> {
                    valueId = 1
                    Log.d("onItemSelected", "valueId = 1")
                }
                'b' -> {
                    valueId = 2
                    Log.d("onItemSelected", "valueId = 2")
                    
                }
                else -> {
                    valueId = 0
                    Log.d("onItemSelected", "valueId = 0")
                }
            }
        }

        override fun onNothingSelected(adapterView: AdapterView<*>?) { return }
    }

旋转设置适配器

val values = arrayOf('Choice : ', 'a', 'b', 'c')  
val adapterSpinner = ArrayAdapter(mContext, android.R.layout.simple_spinner_dropdown_item, values)
            binding.spinner.adapter = adapterSpinner

是否可以使用其他解决方案更改微调器上的值?

android kotlin android-spinner
1个回答
0
投票

我找到了解决方案。可以使用 ListPopupWindow 替代 Android Spinner。我可以更改适配器而无需调用事件 itemChange

© www.soinside.com 2019 - 2024. All rights reserved.