Block the function btprevious2.setOnTouchListener

1

I have an application where I use the btprevious2.setOnTouchListener method to perform quick page switching.

btprevious2.setOnTouchListener(new View.OnTouchListener() {

            private Handler handler;

            @Override 
            public boolean onTouch(View v, MotionEvent event) {

                switch(event.getAction()) {

                    case MotionEvent.ACTION_DOWN:
                        if (handler != null) return true;
                        handler = new Handler();
                        handler.postDelayed(action, 500);
                        break;

                    case MotionEvent.ACTION_UP:
                        if (handler == null) return true;
                        handler.removeCallbacks(action);
                        handler = null;
                        break;
                }
                return false;
            }

            Runnable action = new Runnable() {

                @Override
                public void run() {
                    if(verificacao())
                    {
                        previussalve();
                        return;
                    }else{
                        if (page2>1)
                            page2--;
                        else
                            page2=60;
                        setacursor(page2);
                        escrevebotao();
                        if (page2<10)
                            pageview2.setText("0"+String.valueOf(page2));
                        else pageview2.setText(String.valueOf(page2));

                    }


                    handler.postDelayed(this, 1);
                }
            };
        });

The question is do I want this to only work if another function that I have return false.

The following mode below does not work, but worth the idea

        if(!verificacao())
    {
    btprevious2.setOnTouchListener(new View.OnTouchListener() {

        private Handler handler;

        @Override 
        public boolean onTouch(View v, MotionEvent event) {

            switch(event.getAction()) {

                case MotionEvent.ACTION_DOWN:
                    if (handler != null) return true;
                    handler = new Handler();
                    handler.postDelayed(action, 500);
                    break;

                case MotionEvent.ACTION_UP:
                    if (handler == null) return true;
                    handler.removeCallbacks(action);
                    handler = null;
                    break;
            }
            return false;
        }

        Runnable action = new Runnable() {

            @Override
            public void run() {


                    if (page2>1)
                        page2--;
                    else
                        page2=60;
                    setacursor(page2);
                    escrevebotao();
                    if (page2<10)
                        pageview2.setText("0"+String.valueOf(page2));
                    else pageview2.setText(String.valueOf(page2));




                handler.postDelayed(this, 1);
            }
        };
    });
    }

How do I solve it?

In short, I just want the btprevious2.setOnTouchListener function to work if my check function returns false otherwise I want you to do nothing.

    
asked by anonymous 13.11.2014 / 12:24

2 answers

2

You have to check for the condition within the case to have effect or not, as you wish.

Take the example:

case MotionEvent.ACTION_DOWN:
    if(!verificacao())
    {
        if (handler != null) return true;
        handler = new Handler();
        handler.postDelayed(action, 500);
    } 
    break;
    
13.11.2014 / 12:38
0

I've been able to do this:

Runnable action = new Runnable() {

    @Override
    public void run() {

        if(!verificacao())
        {
            if (page2>1)
                page2--;
            else
                page2=60;
            setacursor(page2);
            escrevebotao();
            if (page2<10)
                pageview2.setText("0"+String.valueOf(page2));
            else pageview2.setText(String.valueOf(page2));
        }

        handler.postDelayed(this, 1);
    }
};

I put the check inside run() , and I blocked all the action. against: the program stays there until the button is released

    
13.11.2014 / 12:53