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

本文出自 “IT徐胖子的專欄” 博客,請務必保留此出處http://woshixy.blog.51cto.com/5637578/1093936


一、單個按鈕點擊事件的監聽

方法一


  1. /**

  2. * 從網絡上獲取圖片

  3. *  

  4. * @author 徐越

  5. *  

  6. */

  7. publicclass MainActivity extends Activity  

  8. {  

  9. private EditText txtPath;  

  10. private Button btnShowImage;  

  11. private ImageView imgView;  

  12. @Override

  13. publicvoid onCreate(Bundle savedInstanceState)  

  14.    {  

  15. super.onCreate(savedInstanceState);  

  16.        setContentView(R.layout.main);  

  17.        txtPath = (EditText) this.findViewById(R.id.txtPath);  

  18.        btnShowImage = (Button) this.findViewById(R.id.btnShowImage);  

  19.        imgView = (ImageView) this.findViewById(R.id.imgView);  

  20.        btnShowImage.setOnClickListener(new ShowImageListener());  

  21.    }  

  22. privatefinalclass ShowImageListener implements View.OnClickListener  

  23.    {  

  24. @Override

  25. publicvoid onClick(View v)  

  26.        {  

  27. // 圖片路徑

  28.            String path = txtPath.getText().toString();  

  29. try

  30.            {  

  31. // 獲取圖片的二進制數據

  32. byte[] imgdata = ImageService.getImage(path);  

  33. // 利用Bitmap工廠生成Bitmap

  34.                Bitmap bitmap = BitmapFactory.decodeByteArray(imgdata, 0, imgdata.length);  

  35. // imageView接收Bitmap並顯示

  36.                imgView.setImageBitmap(bitmap);  

  37.            }  

  38. catch (Exception e)  

  39.            {  

  40.                Toast.makeText(MainActivity.this, "讀取圖片失敗", Toast.LENGTH_SHORT).show();  

  41.            }  

  42.        }  

  43.    }  

  44. }

方法二

在佈局頁面中給該按鈕加上android:onClick="showImage",然後再顯示該元素的Activity中加入showImage(View v)的方法,在該方法中進行操作。



二、多個按鈕點擊事件的監聽

方法一

在Activity中按照第一個大標題的方法,給每個按鈕寫一個監聽類或者監聽方法。

方法二

利用一個監聽器監聽所有按鈕的點擊事件


  1. /**

  2. * 查詢號碼歸屬地

  3. *  

  4. * @author 徐越

  5. *  

  6. */

  7. publicclass MainActivity extends Activity implements View.OnClickListener  

  8. {  

  9. private EditText txtPhone;  

  10. private TextView lblAddress;  

  11. private Button btnQuery;  

  12. private Button btnReset;  

  13. private CallAddressQueryService callAddressQueryService = new CallAddressQueryService();  

  14. privatefinalint CLICK_QUERY = 1;  

  15. privatefinalint CLICK_RESET = 2;  

  16. @Override

  17. publicvoid onCreate(Bundle savedInstanceState)  

  18.    {  

  19. super.onCreate(savedInstanceState);  

  20.        setContentView(R.layout.main);  

  21.        lblAddress = (TextView) this.findViewById(R.id.lblAddress);  

  22.        txtPhone = (EditText) this.findViewById(R.id.txtPhone);  

  23.        btnQuery = (Button) this.findViewById(R.id.btnQuery);  

  24.        btnReset = (Button) this.findViewById(R.id.btnReset);  

  25.        btnQuery.setOnClickListener(this);  

  26.        btnQuery.setTag(CLICK_QUERY);  

  27.        btnReset.setOnClickListener(this);  

  28.        btnReset.setTag(CLICK_RESET);  

  29.    }  

  30. @Override

  31. publicvoid onClick(View v)  

  32.    {  

  33. int tag = (Integer) v.getTag();  

  34. switch (tag)  

  35.        {  

  36. case CLICK_QUERY:  

  37.                query();  

  38. break;  

  39. case CLICK_RESET:  

  40.                reset();  

  41. break;  

  42.        }  

  43.    }  

  44. publicvoid query()  

  45.    {  

  46.        String phone = txtPhone.getText().toString();  

  47. try

  48.        {  

  49.            lblAddress.setText("查詢中");  

  50.            String address = callAddressQueryService.getCallAddress(phone);  

  51.            lblAddress.setText(address);  

  52.        }  

  53. catch (Exception e)  

  54.        {  

  55.            e.printStackTrace();  

  56.            Toast.makeText(this, "查詢失敗", Toast.LENGTH_LONG).show();  

  57.        }  

  58.    }  

  59. publicvoid reset()  

  60.    {  

  61.        txtPhone.setText("");  

  62.        lblAddress.setText("");  

  63.    }  

  64. }


本文出自 “IT徐胖子的專欄” 博客,請務必保留此出處http://woshixy.blog.51cto.com/5637578/1093936


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章