Skip to content

Instantly share code, notes, and snippets.

@codeforfun-jp
Created December 15, 2023 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeforfun-jp/4b5deff9ff26951ee30db00c02617cac to your computer and use it in GitHub Desktop.
Save codeforfun-jp/4b5deff9ff26951ee30db00c02617cac to your computer and use it in GitHub Desktop.
How to use spinner
class MainActivity : AppCompatActivity(), OnItemSelectedListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Spinner>(R.id.spinner).onItemSelectedListener = this
}
// 項目が選択された場合
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
Toast.makeText(this, "${parent?.selectedItem}が選択されました", Toast.LENGTH_SHORT).show()
}
// 項目が選択されなかった場合
override fun onNothingSelected(parent: AdapterView<*>?) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment