码迷,mamicode.com
首页 > 移动开发 > 详细

【转】Android开发20——单个监听器监听多个按钮点击事件

时间:2016-06-14 23:47:12      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

原文网址:http://woshixy.blog.51cto.com/5637578/1093936

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://woshixy.blog.51cto.com/5637578/1093936

一、单个按钮点击事件的监听

方法一

 

  1. /**  
  2.  * 从网络上获取图片  
  3.  *   
  4.  * @author 徐越  
  5.  *   
  6.  */ 
  7. public class MainActivity extends Activity  
  8. {  
  9.     private EditText txtPath;  
  10.     private Button btnShowImage;  
  11.     private ImageView imgView;  
  12.  
  13.     @Override 
  14.     public void onCreate(Bundle savedInstanceState)  
  15.     {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         txtPath = (EditText) this.findViewById(R.id.txtPath);  
  19.         btnShowImage = (Button) this.findViewById(R.id.btnShowImage);  
  20.         imgView = (ImageView) this.findViewById(R.id.imgView);  
  21.         btnShowImage.setOnClickListener(new ShowImageListener());  
  22.     }  
  23.  
  24.     private final class ShowImageListener implements View.OnClickListener  
  25.     {  
  26.         @Override 
  27.         public void onClick(View v)  
  28.         {  
  29.             // 图片路径  
  30.             String path = txtPath.getText().toString();  
  31.             try 
  32.             {  
  33.                 // 获取图片的二进制数据  
  34.                 byte[] imgdata = ImageService.getImage(path);  
  35.                 // 利用Bitmap工厂生成Bitmap  
  36.                 Bitmap bitmap = BitmapFactory.decodeByteArray(imgdata, 0, imgdata.length);  
  37.                 // imageView接收Bitmap并显示  
  38.                 imgView.setImageBitmap(bitmap);  
  39.             }  
  40.             catch (Exception e)  
  41.             {  
  42.                 Toast.makeText(MainActivity.this, "读取图片失败", Toast.LENGTH_SHORT).show();  
  43.             }  
  44.         }  
  45.     }  

方法二

在布局页面中给该按钮加上android:onClick="showImage",然后再显示该元素的Activity中加入showImage(View v)的方法,在该方法中进行操作。

 

 

二、多个按钮点击事件的监听

方法一

在Activity中按照第一个大标题的方法,给每个按钮写一个监听类或者监听方法。

方法二

利用一个监听器监听所有按钮的点击事件

 

  1. /**  
  2.  * 查询号码归属地  
  3.  *   
  4.  * @author 徐越  
  5.  *   
  6.  */ 
  7. public class 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.     private final int CLICK_QUERY = 1;  
  15.     private final int CLICK_RESET = 2;  
  16.  
  17.     @Override 
  18.     public void onCreate(Bundle savedInstanceState)  
  19.     {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         lblAddress = (TextView) this.findViewById(R.id.lblAddress);  
  23.         txtPhone = (EditText) this.findViewById(R.id.txtPhone);  
  24.         btnQuery = (Button) this.findViewById(R.id.btnQuery);  
  25.         btnReset = (Button) this.findViewById(R.id.btnReset);  
  26.         btnQuery.setOnClickListener(this);  
  27.         btnQuery.setTag(CLICK_QUERY);  
  28.         btnReset.setOnClickListener(this);  
  29.         btnReset.setTag(CLICK_RESET);  
  30.     }  
  31.  
  32.     @Override 
  33.     public void onClick(View v)  
  34.     {  
  35.         int tag = (Integer) v.getTag();  
  36.         switch (tag)  
  37.         {  
  38.             case CLICK_QUERY:  
  39.                 query();  
  40.                 break;  
  41.             case CLICK_RESET:  
  42.                 reset();  
  43.                 break;  
  44.         }  
  45.     }  
  46.  
  47.     public void query()  
  48.     {  
  49.         String phone = txtPhone.getText().toString();  
  50.         try 
  51.         {  
  52.             lblAddress.setText("查询中");  
  53.             String address = callAddressQueryService.getCallAddress(phone);  
  54.             lblAddress.setText(address);  
  55.         }  
  56.         catch (Exception e)  
  57.         {  
  58.             e.printStackTrace();  
  59.             Toast.makeText(this, "查询失败", Toast.LENGTH_LONG).show();  
  60.         }  
  61.     }  
  62.  
  63.     public void reset()  
  64.     {  
  65.         txtPhone.setText("");  
  66.         lblAddress.setText("");  
  67.     }  

 

本文出自 “IT徐胖子的专栏” 博客,请务必保留此出处http://woshixy.blog.51cto.com/5637578/1093936

【转】Android开发20——单个监听器监听多个按钮点击事件

标签:

原文地址:http://www.cnblogs.com/wi100sh/p/5585836.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!