Chinaunix首页 | 论坛 | 博客
  • 博客访问: 522783
  • 博文数量: 114
  • 博客积分: 271
  • 博客等级: 二等列兵
  • 技术积分: 733
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-24 13:40
文章分类
文章存档

2014年(5)

2013年(14)

2012年(95)

分类: Java

2012-08-07 22:25:58

实现功能:按下及抬起不同button时button的背景图像不停地切换

点击(此处)折叠或打开

  1. public class UartTestActivity extends Activity {
  2.     UartService uart_srv;
  3.     String message = null;
  4.     ImageButton imagebtn1, imagebtn2, imagebtn3, imagebtn4, imagebtn5;

  5.     /** Called when the activity is first created. */
  6.     @Override
  7.     public void onCreate(Bundle savedInstanceState) {
  8.         super.onCreate(savedInstanceState);
  9.         setContentView(R.layout.main);
  10.         uart_srv = new UartService();
  11.         buttonListener btnliListener1 = new buttonListener();
  12.         buttonListener btnliListener2 = new buttonListener();
  13.         buttonListener btnliListener3 = new buttonListener();
  14.         buttonListener btnliListener4 = new buttonListener();
  15.         buttonListener btnliListener5 = new buttonListener();
  16.         //获取imagebutton对象
  17.         imagebtn1 = (ImageButton) this.findViewById(R.id.imageButton1);
  18.         imagebtn2 = (ImageButton) this.findViewById(R.id.imageButton2);
  19.         imagebtn3 = (ImageButton) this.findViewById(R.id.imageButton3);
  20.         imagebtn4 = (ImageButton) this.findViewById(R.id.imageButton4);
  21.         imagebtn5 = (ImageButton) this.findViewById(R.id.imageButton5);
  22.         //设置touch监听器
  23.         imagebtn1.setOnTouchListener(btnliListener1);
  24.         imagebtn2.setOnTouchListener(btnliListener2);
  25.         imagebtn3.setOnTouchListener(btnliListener3);
  26.         imagebtn4.setOnTouchListener(btnliListener4);
  27.         imagebtn5.setOnTouchListener(btnliListener5);
  28.         //设置click监听器
  29.         imagebtn1.setOnClickListener(btnliListener1);
  30.         imagebtn2.setOnClickListener(btnliListener2);
  31.         imagebtn3.setOnClickListener(btnliListener3);
  32.         imagebtn4.setOnClickListener(btnliListener4);
  33.         imagebtn5.setOnClickListener(btnliListener5);
  34.     }

  35.     public void receive_message(View view) {
  36.         //
  37.         int[] message;
  38.         int i = 0;
  39.         message = uart_srv.ReceiveMessage();
  40.         System.out.println("message length is " + message.length);
  41.         for (i = 0; i < message.length; i++) {
  42.             System.out.println("message[" + i + "]: = " + message[i]);
  43.         }
  44.     }

  45.     class buttonListener implements OnClickListener, OnTouchListener {

  46.         @Override
  47.         public boolean onTouch(View v, MotionEvent event) {
  48.             // TODO Auto-generated method stub
  49.             switch (v.getId()) {
  50.             case R.id.imageButton1:
  51.                 if (event.getAction() == MotionEvent.ACTION_DOWN) {
  52.                     imagebtn1.setBackgroundResource(R.drawable.led_on);
  53.                 }
  54.                 if (event.getAction() == MotionEvent.ACTION_UP) {
  55.                     imagebtn1.setBackgroundResource(R.drawable.led);
  56.                 }
  57.                 break;
  58.             case R.id.imageButton2:
  59.                 if (event.getAction() == MotionEvent.ACTION_DOWN) {
  60.                     imagebtn2.setBackgroundResource(R.drawable.message_on);
  61.                 }
  62.                 if (event.getAction() == MotionEvent.ACTION_UP) {
  63.                     imagebtn2.setBackgroundResource(R.drawable.message);
  64.                 }
  65.                 break;
  66.             case R.id.imageButton3:
  67.                 if (event.getAction() == MotionEvent.ACTION_DOWN) {
  68.                     imagebtn3.setBackgroundResource(R.drawable.fan_on);
  69.                 }
  70.                 if (event.getAction() == MotionEvent.ACTION_UP) {
  71.                     imagebtn3.setBackgroundResource(R.drawable.fan);
  72.                 }
  73.                 break;
  74.             case R.id.imageButton4:
  75.                 if (event.getAction() == MotionEvent.ACTION_DOWN) {
  76.                     imagebtn4.setBackgroundResource(R.drawable.digital_on);
  77.                 }
  78.                 if (event.getAction() == MotionEvent.ACTION_UP) {
  79.                     imagebtn4.setBackgroundResource(R.drawable.digital);
  80.                 }
  81.                 break;
  82.             case R.id.imageButton5:
  83.                 if (event.getAction() == MotionEvent.ACTION_DOWN) {
  84.                     imagebtn5.setBackgroundResource(R.drawable.beep_on);
  85.                 }
  86.                 if (event.getAction() == MotionEvent.ACTION_UP) {
  87.                     imagebtn5.setBackgroundResource(R.drawable.beep);
  88.                 }
  89.                 break;
  90.             }
  91.             return true;
  92.         }

  93.         @Override
  94.         public void onClick(View v) {
  95.             // TODO Auto-generated method stub
  96.             switch(v.getId()){
  97.             case R.id.imageButton1:
  98.                 break;
  99.             case R.id.imageButton2:
  100.                 break;
  101.             case R.id.imageButton3:
  102.                 break;
  103.             case R.id.imageButton4:
  104.                 break;
  105.             case R.id.imageButton5:
  106.                 break;
  107.             }    
  108.         }

  109.     }

阅读(20886) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~