9

我想在搜索栏的拇指上注册一个可点击事件,以便在用户点击它时触发一个事件。可能吗?

4

4 回答 4

3

Combining zwebie and Nermeens answers to the proper solution:

seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    private int mProgressAtStartTracking;
    private final int SENSITIVITY;

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        // handle progress change
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        mProgressAtStartTracking = seekBar.getProgress();
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if(Math.abs(mProgressAtStartTracking - seekBar.getProgress()) <= SENSITIVITY){
            // react to thumb click
        }
    }
});

So this really fires only on thumb click, not on thumb move and not on click on other parts of the seekbar.

You can adjust sensitivity, because sometimes a click already moves the thumb a little bit and allowing little changes while still clicking is less frustrating. A good value here depends on the size of the seekbar and the max value it can have. For me 3 worked well on a full-width seekbar with 50 max on a portrait layout.

于 2016-08-24T12:55:14.377 回答
1

我能够通过以下方式实现这一目标:

seekBar.setOnTouchListener(new OnTouchListener() 
{
    @Override
    public boolean onTouch(View v, MotionEvent event) 
    {
        if(event.getAction() == MotionEvent.ACTION_MOVE)
        {
            changedPosition = true;
            seekBar.setProgress(seekBar.getProgress());
            return false;
        }
        else if (event.getAction() == MotionEvent.ACTION_UP)
        {
            if(!changedPosition)
            {
                  //do action here
            }
        }
   }

希望这可以帮助

于 2012-08-14T11:33:51.133 回答
1

我以 Bijom 的回答为灵感。这将在拇指移动时触发,因此如果用户不点击拇指它就不会移动。希望这可以帮助。:)

如果您对它的工作原理有任何疑问,请随时发表评论。

    mSeekBarSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        int progress = mSeekBarSpeed.getProgress();

        boolean started = false; //use this variable to see whether the user clicked the right place


        @Override
        public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
            if(!started){ //check to see if user clicks the right place
                //if the user clicks within a specific threshold
                float threshold = (float)seekBar.getMax() / seekBar.getWidth() * seekBar.getThumb().getIntrinsicWidth() / 2;
                if(Math.abs(progressValue - progress) < threshold){
                     if(fromUser){ //checks if user actually clicked it
                         started = true;
                     }

                }else{
                    seekBar.setProgress(progress); //set to original progress
                    onStopTrackingTouch(seekBar); //not really necessary, keep or delete based on your needs
                    return; //get out of method
                }
            }

            if(started) {

                progress = progressValue; //update progress variable
                System.out.println("onProgressChanged:" + progress + "/" + seekBar.getMax());

                //DO WHAT YOU NEED TO DO WHEN PROGRESS IS CHANGING

            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            System.out.println("onStartTracking:" + progress + "/" + seekBar.getMax());
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            System.out.println("onStopTracking:" + progress + "/" + seekBar.getMax());
            //DO WHATEVER YOU NEED TO DO WHEN PROGRESS IS DONE CHANGING

            started = false; //remember to set variable to false
        }

    });
于 2017-07-20T22:31:19.207 回答
0

我这样做:

pagesSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { //listener
        @Override
        public void onStopTrackingTouch(final SeekBar seekBar) {
            //add your event here
        }

        @Override
        public void onStartTrackingTouch(final SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(final SeekBar seekBar, final int progress, final boolean fromUser) {
                updateControls(progress, false);
        }
    });
于 2012-07-27T08:52:26.940 回答