android kotlin spinner适用于API 23,但不适用于API 21

时间:2018-07-24 09:35:31

标签: android kotlin spinner

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

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

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文件: enter image description here

enter image description here

enter image description here     在此处输入代码

1 个答案:

答案 0 :(得分:0)

我找到了答案

android:spinnerMode =“ dialog”

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