Question

I'm trying to implement onTouch in my class used to remote control a mindstorms robot. I've still a lot of work to do, but for now I'm trying to tidy up the direct control which uses onClick. 5 buttons, 5 instantiations like the code below, which calls one of 5 methods containing the instructions for the robot to move.

EDIT: An Activity has 5 buttons, each does something. The original class used onClickListener as shown below, where they would be instantiated within the OnCreate method calling a void method that had the actual code to perform.

I wanted to use onTouch instead as it makes the remote ...better. But I had a problem trying to get it to work with more than one button.

btn1 = (Button) findViewById(R.id.btn1);// instantiates a button called
    // btn1 one from the xml
    btn1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            driveFore();//move forward

        }// calls the method
    });// end of method

This is the original onClick, which calls a method outside the onCreate.

private void driveFore() {
    // TODO Auto-generated method stub

    Motor.A.forward();
    Motor.B.forward();

}//Move forward

I would like to do the above, but with onTouch. As it is, once a button is clicked, the motors continue until another button is clicked, so I thought that onTouch would be better since it will only move as long as a button is held down.

And this is the onTouch variant

btn1 = (Button) findViewById(R.id.btn1);
    btn1.setOnTouchListener(this);

Which listens for

 public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    switch (event.getAction()) {
     case MotionEvent.ACTION_DOWN:
            Motor.A.forward();
            Motor.B.forward();
            break;
     case MotionEvent.ACTION_UP:{

            Motor.A.flt();
            Motor.B.flt();
    }
    break;
}
    return true;
}

The above code works, but only for 1 button. How would I go about applying the above for as many as 5 buttons.

EDIT: As suggested I've tried using these two method:

            btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnTouchListener(new OnTouchListener(){
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Motor.A.forward();
                Motor.B.forward();
                break;
             case MotionEvent.ACTION_UP:
                Motor.A.flt();
                Motor.B.flt();
             }
            return true;

        }
    });


    btn2 = (Button) findViewById(R.id.btn2);
    btn2.setOnTouchListener(new OnTouchListener(){
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Motor.A.forward();
                Motor.B.forward();
                break;
             case MotionEvent.ACTION_UP:
                Motor.A.flt();
                Motor.B.flt();
             }
            return true;
        }
    });

Works just fine. Thanks guys.

Was it helpful?

Solution

You don't need to have your Activity extends OnTouchListener. You can do the same thing with anonymous inner classes. Like this:

btn1.setOnTouchListener(new OnTouchListener(){
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Motor.A.forward();
            Motor.B.forward();
            break;
         case MotionEvent.ACTION_UP:
            Motor.A.flt();
            Motor.B.flt();
         }
    }
});

btn2.setOnTouchListener(new OnTouchListener(){
    public boolean onTouch(View v, MotionEvent event) {
        // Something else here
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top