Skip to content

Instantly share code, notes, and snippets.

@codeforfun-jp
Created October 4, 2023 10:06
Show Gist options
  • Save codeforfun-jp/defed06fd2333e6e30cd591e8492d04c to your computer and use it in GitHub Desktop.
Save codeforfun-jp/defed06fd2333e6e30cd591e8492d04c to your computer and use it in GitHub Desktop.
How to set button click event with kotlin 5
class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.btn1).setOnClickListener(this)
findViewById<Button>(R.id.btn2).setOnClickListener(this)
findViewById<Button>(R.id.btn3).setOnClickListener(this)
}
override fun onClick(v: View) {
when (v.id) {
R.id.btn1 -> {
Snackbar.make(v, "ボタン1が押されました", Snackbar.LENGTH_SHORT).show()
}
R.id.btn2 -> {
Snackbar.make(v, "ボタン2が押されました", Snackbar.LENGTH_SHORT).show()
}
R.id.btn3 -> {
Snackbar.make(v, "ボタン3が押されました", Snackbar.LENGTH_SHORT).show()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment