Continuously increase integer value as the button is pressed

后端 未结 9 1285
长情又很酷
长情又很酷 2020-12-04 11:10

I\'m new to Android so sorry if the question is easy to answer. I have two buttons, a decrease and an increase button, and in the middle of them a TextView which displays a

相关标签:
9条回答
  • 2020-12-04 11:32

    My way to increment value on long click is to use Timer used to check periodically if button is still pressed and than increase value, otherwise cancel timer. To update UI use Handler.

    vh.bttAdd.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
    
                final Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                     if(vh.bttAdd.isPressed()) {
                         final int track = ((ChannelAudioTrack) channels.get(vh.getAdapterPosition())).goToNextTrack();
                      updateUI(vh,track);
                     }
                    else
                    timer.cancel();
                }
                },100,200);
    
                return true;
            }
        });
    

    Handler:

    private void updateUI(final TrackViewHolder vh, final int track)
     {
    new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                      vh.tvTrackNumber.setText(Integer.toString(track));
                            }
                        }) ;
    }
    
    0 讨论(0)
  • 2020-12-04 11:36

    Best and easy solution i created ,check it out it works for me

    public void increment() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                check_amount = check_amount + 100;// increment by 100
                tv_balance.setText("" + check_amount); // show continues incrementing value
                if (check_amount != 5000) {
                    increment();
                }
            }
        }, 1); //1 ms for fast incrementing
    }
    
    0 讨论(0)
  • 2020-12-04 11:38

    While the accepted answer is totally correct, it can be simplified a bit.

    Basically, we can optimize two things:

    • We don't need the OnTouchListener.
    • We can instantiate the runnable object just once instead of creating multiple objects.

    So this is my version:

    // global variables
    Handler handler = new Handler();
    Runnable runnable;
    
    increaseView.setOnLongClickListener(new View.OnLongClickListener() {
    
        @Override
        public boolean onLongClick(View v) {
    
            runnable = new Runnable() {
                @Override
                public void run() {
                    if (!increaseView.isPressed()) return;
                    increaseValue();
                    handler.postDelayed(runnable, DELAY);
                }
            };
    
            handler.postDelayed(runnable, DELAY);
            return true;
    
        }
    
    });
    

    Here the runnable object is reused. And when the view is not pressed anymore, it will stop calling itself.

    The decrease view or button can be defined in a similar way.

    0 讨论(0)
提交回复
热议问题