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
是否可以使用其他解决方案更改微调器上的值?
我找到了解决方案。可以使用 ListPopupWindow 替代 Android Spinner。我可以更改适配器而无需调用事件 itemChange