android kotlin spinner适用于API 23,但不适用于API 21 [英] android kotlin spinner working for API 23, but not working for API 21

查看:101
本文介绍了android kotlin spinner适用于API 23,但不适用于API 21的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在真实设备SM-G900F API 23中测试了微调器,它运行良好.但是它在SM-N9005 API 21中不起作用.它显示了微调器,但是当我单击微调器时它没有显示任何选项.

I test my spinner in real device SM-G900F API 23, it is working well. But it is not working in SM-N9005 API 21. It shows the spinner but it did not show any options when I click the spinner.

请在下面查看我的代码,以帮助解决此问题.

Please see my code below and help to solve this issue.

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

        val spinnerOne = search_spinner1
        val spinnerTwo = search_spinner2
        val userDetail = UserDetail(this)
        //SpinnerOne
        spinnerOne.adapter = ArrayAdapter<String>(this, R.layout.item, countryArray)
        //SpinnerTwo
        val dataAdapter = ArrayAdapter<String>(this, R.layout.item, tempArray)
        spinnerTwo.setAdapter(dataAdapter)
        spinnerOne.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                countrySelected = parent?.getItemAtPosition(position).toString()
                citySelected = countrySelected
                if (countrySelected == "香港") {
                    userDetail.saveCountry(countrySelected)
                    userDetail.saveCity(countrySelected)
                    spinnerTwo.visibility = View.GONE
                    finish()
                } else if (countrySelected == "澳門") {
                    userDetail.saveCountry(countrySelected)
                    userDetail.saveCity(countrySelected)
                    spinnerTwo.visibility = View.GONE
                    finish()
                } else if (countrySelected == "台灣") {
                    userDetail.saveCountry(countrySelected)
                    tempArray.clear()
                    tempArray.addAll(taiwanArray)
                    dataAdapter.notifyDataSetChanged()
                    spinnerTwo.visibility = View.VISIBLE
                } 
            }
        }

        //hide spinner2
        spinnerTwo.visibility = View.GONE
        spinnerTwo.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                citySelected = parent?.getItemAtPosition(position).toString()
                if (citySelected == "選擇城市") {
                } else {
                    userDetail.saveCity(citySelected)
                    finish()
                }
            }
        }

以下是xml文件:

Here with the xml file:

在此处输入代码

enter code here

推荐答案

我找到了答案

android:spinnerMode ="dialog"

android:spinnerMode="dialog"

添加此代码,它将适用于API 21和23

add this code it will be work for API 21 and 23

这篇关于android kotlin spinner适用于API 23,但不适用于API 21的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆