DBILITY

안드로이드 spinner(combo) 사용법 본문

android

안드로이드 spinner(combo) 사용법

DBILITY 2024. 3. 4. 16:54
반응형

 

class MainActivity : AppCompatActivity() {

    private val binding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    private var list = listOf("--선택하세요--","사과","바나나","귤")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //enableEdgeToEdge()
        setContentView(binding.root)
        /*ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }*/

        var spinnerAdapter = ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list)
        with(binding) {
            spinner.apply {
                adapter = spinnerAdapter
                onItemSelectedListener = object : OnItemSelectedListener {
                    override fun onItemSelected(
                        parent: AdapterView<*>?,
                        view: View?,
                        position: Int,
                        id: Long
                    ) {
                        var selectedItem = list[position]
                        tvSpinnerSelect.text = selectedItem
                    }

                    override fun onNothingSelected(parent: AdapterView<*>?) {
                        Log.d("onNothingSelected","이건 언제 동작하는겨?")
                    }
                }
            }
        }


    }
}

반응형
Comments