2014-01-31 2 views
-1

이 코드는 어떻게 하나의 버튼에만 사용할 수 있습니까? 나는 이것을 바꿀 수 없다!하나의 버튼에만이 코드를 사용할 수 있습니까?

 
    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

? 약간의 설명이 도움이 될 것입니다. –

+1

버튼의 onTouchListener를 구현합니다. – Piyush

+0

화면의 다른 위치에서 버튼을 드래그하여 버튼을 터치하면이 이벤트가 어떻게됩니까? 내게 샘플을주세요. – user3257138

답변

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; 
       } 
      }); 
+0

아니, 이걸 원하지 않아. 이 코드는 버튼의 다른 화면 상태에서 손가락을 드래그하면 작동하지 않습니다. – user3257138

관련 문제