[安卓] 2、使用2中方法做按鈕監聽和圖片按鈕使用

來源:互聯網
上載者:User

標籤:

 

 

 

 

第一種方法是使用點擊監聽器來實現(代碼中注釋掉的部分):這種方法要在初始化的函數中將按鈕綁定在點擊監聽器上(23,24)btn_ok.setOnClickListener(this);。然後處理統一寫在抽象函數onClick(View v) 中,並用v == btn_ok來判別是哪一個按鈕的點擊。(28~34)

第二種方法是使用內部類實現按鍵監聽,具體如下(這個看起來要代碼多一點,各個處理是單獨的)

 1 package com.himi.button;//包路徑 2 //import匯入類庫 3  4 import android.app.Activity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 import android.widget.TextView;10 11 /* 使用點擊監聽器介面進行監聽12  public class MainActivity extends Activity implements OnClickListener {//使用點擊監聽器13      private Button btn_ok, btn_cancel;14      private TextView tv;15     16      @Override17      public void onCreate(Bundle savedInstanceState) {18          super.onCreate(savedInstanceState);19          setContentView(R.layout.main);20          btn_ok = (Button) findViewById(R.id.btn_ok);21          btn_cancel = (Button) findViewById(R.id.btn_cancel);22          tv = (TextView) findViewById(R.id.tv);23          btn_ok.setOnClickListener(this);//將btn_ok按鈕綁定在點擊監聽器上24          btn_cancel.setOnClickListener(this);//將btn_cancel按鈕綁定在點擊監聽器上25      }26     27      @Override28      public void onClick(View v) {//使用監聽器就要重寫其抽象函數 29          if (v == btn_ok) {30              tv.setText("確定按鈕觸發事件!");31          } else if (v == btn_cancel) {32              tv.setText("取消按鈕觸發事件!");33          }34     }35  }36  */37 //內部類實現按鍵監聽38 public class MainActivity extends Activity {// 使用點擊監聽器39     private Button btn_ok, btn_cancel;40     private TextView tv;41 42     @Override43     public void onCreate(Bundle savedInstanceState) {44         super.onCreate(savedInstanceState);45         setContentView(R.layout.main);46         btn_ok = (Button) findViewById(R.id.btn_ok);47         btn_cancel = (Button) findViewById(R.id.btn_cancel);48         tv = (TextView) findViewById(R.id.tv);49         btn_ok.setOnClickListener(new OnClickListener() {// 將btn_ok按鈕綁定在點擊監聽器上50             @Override51             public void onClick(View v) {52                 tv.setText("確定按鈕觸發事件!");53             }54         });55         btn_cancel.setOnClickListener(new OnClickListener() {// 將btn_cancel按鈕綁定在點擊監聽器上56             @Override57             public void onClick(View v) {58                 tv.setText("取消按鈕觸發事件!");59             }60         });61     }62 63 }

 

理解了上面的下面的圖片按鈕也就很簡單啦:如下第9行可見我這裡用的不是滑鼠的點擊監聽了,而是觸屏監聽。其中根據觸屏事件來更換ImageButton的背景來實現上述效果。這裡的圖片資源是存起來的,通過調用getResources().getDrawable(R.drawable.xxx)來擷取資源。

 1 public class MainActivity extends Activity { 2     private ImageButton Ibtn; 3     @Override 4     public void onCreate(Bundle savedInstanceState) { 5         super.onCreate(savedInstanceState); 6         setContentView(R.layout.main); 7         Ibtn = (ImageButton)findViewById(R.id.imageBtn);  8         //為圖片按鈕添加觸屏監聽 9         Ibtn.setOnTouchListener(new OnTouchListener() {10             @Override11             public boolean onTouch(View v, MotionEvent event) {12                 //使用者當前為按下13                 if(event.getAction()==MotionEvent.ACTION_DOWN){14                     //設定圖片按鈕背景圖15                     Ibtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.press));16                 //使用者當前為抬起17                 }else if(event.getAction()==MotionEvent.ACTION_UP){18                     Ibtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.nopress));19                 }20                 return false;21             }22         });23     }24 }

注意:這裡的按鈕是ImageButton!

 

 

 

  

本文連結:http://www.cnblogs.com/zjutlitao/p/4229564.html

更多精彩:http://www.cnblogs.com/zjutlitao

 

[安卓] 2、使用2中方法做按鈕監聽和圖片按鈕使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.