本文出自 “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. // p_w_picpathView接收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