Seekbar的拇指点击

时间:2012-07-27 08:49:32

标签: android seekbar

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

4 个答案:

答案 0 :(得分:3)

将zwebie和Nermeens结合起来,找到合适的解决方案:

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
        }
    }
});

所以这只会在拇指点击时触发,而不是在拇指移动时触发,而不是点击搜索栏的其他部分。

您可以调整灵敏度,因为有时点击已经略微移动了拇指并且在点击时允许很少的更改不那么令人沮丧。这里一个好的值取决于搜索条的大小和它可以具有的最大值。对于我来说,3在全宽搜索栏上工作得很好,在纵向布局上最多50个。

答案 1 :(得分: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
            }
        }
   }

希望这会有所帮助

答案 2 :(得分: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
        }

    });

答案 3 :(得分: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);
        }
    });