Lock btprevious2.setOnTouchListener function

Asked

Viewed 53 times

1

I have an application where I use the method btprevious2.setOnTouchListener to make the page change quickly.

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 point is I want this to only work if another function I have to return false.

The following way below does not work, but is 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 resolve?

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

  • It’s hard to understand what you want to do, but from what I understand, you have to put the condition if (!verificacao()) involving the switch(event.getAction()) that is, as soon as you enter the function onTouch().

  • paul managed to do different by blocking the action. put the if inside the run

2 answers

2


You have to put the verification condition inside the case so that it has 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;
  • I ended up putting inside the run but so it works tbm I believe I’ll test

0

I got it this way:

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 the run(), and blocked all the action. against: the program stays there until the button is released

Browser other questions tagged

You are not signed in. Login or sign up in order to post.