# Android: 按鈕事件監聽器 Button Listener ## Key points ### 1. 定義局部變數(例如 private ImageButton btn1;) ### 2. 按鈕監聽器(按鈕被按下時,要做的處理) eg. 下方的程式,會彈出文字訊息 ``` Toast.makeText(getApplicationContext(), "!!水餃!!", Toast.LENGTH_SHORT).show(); ``` ### 3. 在 onCreate() 中,綁定局部變數(Step1)和 layout 變數(已定義的變數) ``` btn1 = (ImageButton)findViewById(R.id.imageButton); ``` ### 4. 在 onCreate() 中,綁定 Step2 定義的按鈕監聽器 ## 參考教學 https://atceiling.blogspot.com/2021/06/android14ui-imageview-imagebutton.html