Peter Panne
Peter Panne

Reputation: 485

How to catch the OnTouchEvent just in a particular situation?

I am about to write a little quiz app. I have 4 Buttons which represent the possibilities of answers. When one button is depressed It should blink red or green to indicate right or wrong. So far no problem.

But the next step should be to slide out the old question and to slide in the new one. This should initiate by a touch of the user anywhere on the screen.

How can I achieve this? Which methods I have to use?

Code:

@Override
public void onClick(View v) {
    Button button = (Button)findViewById(v.getId());

    if(button.getText().equals(quizWorld.getCurrentQuestion().getRightAnswer())){
        //right
    }else{
        //wrong
    }
}

@Override
public boolean  onTouchEvent(MotionEvent event){
    switch(event.getAction()){
        case MotionEvent.ACTION_UP:
            //Game Over?
            if(quizWorld.swapQuestion()==false){
                viewFlipper.setDisplayedChild(1);
            }
            break;
    }
    return true;    
}

Just for Explanation: In the onClick the Programm compares the text on the button with the saved answer and initiate the blinking (not coded yet). And on the onTouchEvent it changes the question.

And now: onTouchEvent should be only "activated" when the Programm has just finished the checking process. After that the Programm shouldn't react on OnEventTouch.

Upvotes: 0

Views: 53

Answers (1)

JamieB
JamieB

Reputation: 923

If you don't need to handle the touch event, just return false onTouchEvent and let the touch pass through.

@Override
public boolean  onTouchEvent(MotionEvent event){
if (I don't need this touch)
{
   return false;
}
    switch(event.getAction()){
    case MotionEvent.ACTION_UP:
        //Game Over?
        if(quizWorld.swapQuestion()==false){
            viewFlipper.setDisplayedChild(1);
        }
        break;
    }
    return true;    
}

Upvotes: 1

Related Questions