Android開發——單個監聽器監聽多個按鈕點擊事件

來源:互聯網
上載者:User

一、單個按鈕點擊事件的監聽   方法一 [java]  /**  * 從網路上擷取圖片  *   * @author 徐越  *   */  public class MainActivity extends Activity  {      private EditText txtPath;      private Button btnShowImage;      private ImageView imgView;        @Override      public void onCreate(Bundle savedInstanceState)      {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          txtPath = (EditText) this.findViewById(R.id.txtPath);          btnShowImage = (Button) this.findViewById(R.id.btnShowImage);          imgView = (ImageView) this.findViewById(R.id.imgView);          btnShowImage.setOnClickListener(new ShowImageListener());      }        private final class ShowImageListener implements View.OnClickListener      {          @Override          public void onClick(View v)          {              // 圖片路徑               String path = txtPath.getText().toString();              try              {                  // 擷取圖片的位元據                   byte[] imgdata = ImageService.getImage(path);                  // 利用Bitmap工廠產生Bitmap                   Bitmap bitmap = BitmapFactory.decodeByteArray(imgdata, 0, imgdata.length);                  // imageView接收Bitmap並顯示                   imgView.setImageBitmap(bitmap);              }              catch (Exception e)              {                  Toast.makeText(MainActivity.this, "讀取圖片失敗", Toast.LENGTH_SHORT).show();              }          }      }  }   /** * 從網路上擷取圖片 *  * @author 徐越 *  */public class MainActivity extends Activity{private EditText txtPath;private Button btnShowImage;private ImageView imgView; @Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);txtPath = (EditText) this.findViewById(R.id.txtPath);btnShowImage = (Button) this.findViewById(R.id.btnShowImage);imgView = (ImageView) this.findViewById(R.id.imgView);btnShowImage.setOnClickListener(new ShowImageListener());} private final class ShowImageListener implements View.OnClickListener{@Overridepublic void onClick(View v){// 圖片路徑String path = txtPath.getText().toString();try{// 擷取圖片的位元據byte[] imgdata = ImageService.getImage(path);// 利用Bitmap工廠產生BitmapBitmap bitmap = BitmapFactory.decodeByteArray(imgdata, 0, imgdata.length);// imageView接收Bitmap並顯示imgView.setImageBitmap(bitmap);}catch (Exception e){Toast.makeText(MainActivity.this, "讀取圖片失敗", Toast.LENGTH_SHORT).show();}}}}方法二 在布局頁面中給該按鈕加上android:onClick="showImage",然後再顯示該元素的Activity中加入showImage(View v)的方法,在該方法中進行操作。     二、多個按鈕點擊事件的監聽   方法一 在Activity中按照第一個大標題的方法,給每個按鈕寫一個監聽類或者監聽方法。   方法二 利用一個監聽器監聽所有按鈕的點擊事件 [java]  /**  * 查詢號碼歸屬地  *   * @author 徐越  *   */  public class MainActivity extends Activity implements View.OnClickListener  {      private EditText txtPhone;      private TextView lblAddress;      private Button btnQuery;      private Button btnReset;      private CallAddressQueryService callAddressQueryService = new CallAddressQueryService();      private final int CLICK_QUERY = 1;      private final int CLICK_RESET = 2;        @Override      public void onCreate(Bundle savedInstanceState)      {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          lblAddress = (TextView) this.findViewById(R.id.lblAddress);          txtPhone = (EditText) this.findViewById(R.id.txtPhone);          btnQuery = (Button) this.findViewById(R.id.btnQuery);          btnReset = (Button) this.findViewById(R.id.btnReset);          btnQuery.setOnClickListener(this);          btnQuery.setTag(CLICK_QUERY);          btnReset.setOnClickListener(this);          btnReset.setTag(CLICK_RESET);      }        @Override      public void onClick(View v)      {          int tag = (Integer) v.getTag();          switch (tag)          {              case CLICK_QUERY:                  query();                  break;              case CLICK_RESET:                  reset();                  break;          }      }        public void query()      {          String phone = txtPhone.getText().toString();          try          {              lblAddress.setText("查詢中");              String address = callAddressQueryService.getCallAddress(phone);              lblAddress.setText(address);          }          catch (Exception e)          {              e.printStackTrace();              Toast.makeText(this, "查詢失敗", Toast.LENGTH_LONG).show();          }      }        public void reset()      {          txtPhone.setText("");          lblAddress.setText("");      }  }   /** * 查詢號碼歸屬地 *  * @author 徐越 *  */public class MainActivity extends Activity implements View.OnClickListener{private EditText txtPhone;private TextView lblAddress;private Button btnQuery;private Button btnReset;private CallAddressQueryService callAddressQueryService = new CallAddressQueryService();private final int CLICK_QUERY = 1;private final int CLICK_RESET = 2; @Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);lblAddress = (TextView) this.findViewById(R.id.lblAddress);txtPhone = (EditText) this.findViewById(R.id.txtPhone);btnQuery = (Button) this.findViewById(R.id.btnQuery);btnReset = (Button) this.findViewById(R.id.btnReset);btnQuery.setOnClickListener(this);btnQuery.setTag(CLICK_QUERY);btnReset.setOnClickListener(this);btnReset.setTag(CLICK_RESET);} @Overridepublic void onClick(View v){int tag = (Integer) v.getTag();switch (tag){case CLICK_QUERY:query();break;case CLICK_RESET:reset();break;}} public void query(){String phone = txtPhone.getText().toString();try{lblAddress.setText("查詢中");String address = callAddressQueryService.getCallAddress(phone);lblAddress.setText(address);}catch (Exception e){e.printStackTrace();Toast.makeText(this, "查詢失敗", Toast.LENGTH_LONG).show();}} public void reset(){txtPhone.setText("");lblAddress.setText("");}}  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.