Android开发20——单个监听器监听多个按钮点击事件
原文网址:http://woshixy.blog.51cto.com/5637578/1093936
一、单个按钮点击事件的监听
方法一
- /**
- * 从网络上获取图片
- *
- * @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();
- }
- }
- }
- }
方法二
在布局页面中给该按钮加上android:onClick="showImage",然后再显示该元素的Activity中加入showImage(View v)的方法,在该方法中进行操作。
二、多个按钮点击事件的监听
方法一
在Activity中按照第一个大标题的方法,给每个按钮写一个监听类或者监听方法。
方法二
利用一个监听器监听所有按钮的点击事件
- /**
- * 查询号码归属地
- *
- * @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("");
- }
- }
本文出自 “IT徐胖子的专栏” 博客,请务必保留此出处http://woshixy.blog.51cto.com/5637578/1093936
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。