android button 按下及抬起事件监听器设置


实现功能:按下及抬起不同button时button的背景图像不停地切换
public class UartTestActivity extends Activity {
    UartService uart_srv;
    String message = null;
    ImageButton imagebtn1, imagebtn2, imagebtn3, imagebtn4, imagebtn5;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        uart_srv = new UartService();
        buttonListener btnliListener1 = new buttonListener();
        buttonListener btnliListener2 = new buttonListener();
        buttonListener btnliListener3 = new buttonListener();
        buttonListener btnliListener4 = new buttonListener();
        buttonListener btnliListener5 = new buttonListener();
        //获取imagebutton对象
        imagebtn1 = (ImageButton) this.findViewById(R.id.imageButton1);
        imagebtn2 = (ImageButton) this.findViewById(R.id.imageButton2);
        imagebtn3 = (ImageButton) this.findViewById(R.id.imageButton3);
        imagebtn4 = (ImageButton) this.findViewById(R.id.imageButton4);
        imagebtn5 = (ImageButton) this.findViewById(R.id.imageButton5);
        //设置touch监听器
        imagebtn1.setOnTouchListener(btnliListener1);
        imagebtn2.setOnTouchListener(btnliListener2);
        imagebtn3.setOnTouchListener(btnliListener3);
        imagebtn4.setOnTouchListener(btnliListener4);
        imagebtn5.setOnTouchListener(btnliListener5);
        //设置click监听器
        imagebtn1.setOnClickListener(btnliListener1);
        imagebtn2.setOnClickListener(btnliListener2);
        imagebtn3.setOnClickListener(btnliListener3);
        imagebtn4.setOnClickListener(btnliListener4);
        imagebtn5.setOnClickListener(btnliListener5);
    }
    public void receive_message(View view) {
        //
        int[] message;
        int i = 0;
        message = uart_srv.ReceiveMessage();
        System.out.println("message length is " + message.length);
        for (i = 0; i < message.length; i++) {
            System.out.println("message[" + i + "]: = " + message[i]);
        }
    }
    class buttonListener implements OnClickListener, OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.imageButton1:
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    imagebtn1.setBackgroundResource(R.drawable.led_on);
                }
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    imagebtn1.setBackgroundResource(R.drawable.led);
                }
                break;
            case R.id.imageButton2:
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    imagebtn2.setBackgroundResource(R.drawable.message_on);
                }
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    imagebtn2.setBackgroundResource(R.drawable.message);
                }
                break;
            case R.id.imageButton3:
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    imagebtn3.setBackgroundResource(R.drawable.fan_on);
                }
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    imagebtn3.setBackgroundResource(R.drawable.fan);
                }
                break;
            case R.id.imageButton4:
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    imagebtn4.setBackgroundResource(R.drawable.digital_on);
                }
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    imagebtn4.setBackgroundResource(R.drawable.digital);
                }
                break;
            case R.id.imageButton5:
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    imagebtn5.setBackgroundResource(R.drawable.beep_on);
                }
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    imagebtn5.setBackgroundResource(R.drawable.beep);
                }
                break;
            }
            return true;
        }
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch(v.getId()){
            case R.id.imageButton1:
                break;
            case R.id.imageButton2:
                break;
            case R.id.imageButton3:
                break;
            case R.id.imageButton4:
                break;
            case R.id.imageButton5:
                break;
            }    
        }
    }

你可能感兴趣的:(android button 按下及抬起事件监听器设置)