2014-01-31 80 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 for按鈕。 – 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

相關問題