我如何才能将此代码仅用于一个按钮?

时间:2014-01-31 11:48:07

标签: android sample

我如何才能将此代码仅用于一个按钮? 我不能改变这个!

   public boolean onTouchEvent(MotionEvent event)
    {
        boolean[] newButtonStates = new boolean[24];

int action = event.getAction(); boolean isDownAction = (action & 0x36) == 0x36 || action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE; for (int touchIndex = 0; touchIndex < event.getPointerCount(); touchIndex++) { // find button for location int x = (int) event.getX(touchIndex); int y = (int) event.getY(touchIndex); for (int buttonIndex = 0; buttonIndex < buttons.size(); buttonIndex++) { View button = buttons.get(buttonIndex); int[] location = new int[2]; button.getLocationOnScreen(location); int buttonX = location[0]; int buttonY = location[1]; Rect rect = new Rect(buttonX, buttonY, buttonX + button.getWidth(), buttonY + button.getHeight()); if (rect.contains(x, y)) { newButtonStates[buttonIndex] = isDownAction; break; } } } for (int index = 0; index < newButtonStates.length; index++) { if (buttonStates[index] != newButtonStates[index]) { buttonStates[index] = newButtonStates[index]; View button = buttons.get(index); toggleButtonSound(button, newButtonStates[index]); } } return true;

1 个答案:

答案 0 :(得分:0)

我认为你需要这样做:

Button myBtn = (Button) findViewById(R.id.btn);    
myBtn.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_UP){

                        // Do what you want
                        return true;
                    }
                    return false;
                }
            });