Android Seekbar – 只允许用拇指进行更改?

我有一个seekbar,我只想允许更改拇指/手柄部分。 如果用户点击其他任何地方,我想这个水龙头被忽略。

有没有无形的财产要做到这一点? 如果没有,我已经知道我可以设置一个onTouch监听器,并返回true来“禁用”它,但是有没有一种方法来检测拇指是什么时候点击,以便我可以告诉onTouch在这种情况下返回false

Solutions Collecting From Web of "Android Seekbar – 只允许用拇指进行更改?"

我假设你想改变拇指位置通过代码,这是用户restricted.so为此尝试this.may可以帮助你。

  myseekBar.setOnSeekBarChangeListener( new OnSeekBarChangeListener() { int originalProgress; @Override public void onStopTrackingTouch(SeekBar seekBar) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { originalProgress = seekBar.getProgress(); } @Override public void onProgressChanged(SeekBar seekBar, int arg1, boolean fromUser) { if( fromUser == true){ seekBar.setProgress(originalProgress); } } }); 

我有同样的问题,我修复了一个简单的逻辑来比较新的Progress X OldProgress。 这是我的代码到一个VerticalSlider,只要用户点击Thumb时允许更改值。

这是一个窍门:

 int tempProgress = (getMax() - (int) (getMax() * event.getY() / getHeight())); int diff = Math.abs(tempProgress - progress); if ( diff > 7 && !isMoving) return true; isMoving = true; 

这是所有的组件来源:

  package android.widget; import android.content.Context; import android.graphics.Canvas; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.MotionEvent; public class VerticalSeekBar extends SeekBar { private OnSeekBarChangeListener onChangeListener; private int progress = 0; private Drawable mThumb; private boolean isMoving; public VerticalSeekBar(Context context) { super(context); } public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public VerticalSeekBar(Context context, AttributeSet attrs) { super(context, attrs); } protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(h, w, oldh, oldw); } @Override protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(heightMeasureSpec, widthMeasureSpec); setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth()); } protected void onDraw(Canvas c) { c.rotate(-90); c.translate(-getHeight(), 0); super.onDraw(c); } @Override public void setOnSeekBarChangeListener(OnSeekBarChangeListener onChangeListener){ this.onChangeListener = onChangeListener; } public void setThumb(Drawable thumb) { if (thumb != null) { thumb.setCallback(this); } super.setThumb(thumb); mThumb = thumb; mThumb.setCallback(this); } @Override public boolean onTouchEvent(MotionEvent event) { if (!isEnabled()) { return false; } switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if ( onChangeListener!= null)onChangeListener.onStartTrackingTouch(this); setPressed(true); setSelected(true); break; case MotionEvent.ACTION_MOVE: int tempProgress = (getMax() - (int) (getMax() * event.getY() / getHeight())); int diff = Math.abs(tempProgress - progress); if ( diff > 7 && !isMoving) return true; isMoving = true; progress = getMax() - (int) (getMax() * event.getY() / getHeight()); if(progress < 18) {progress = 18;} if(progress > getMax()) {progress = getMax();} setProgressAndThumb(progress); setPressed(true); setSelected(true); break; case MotionEvent.ACTION_UP: isMoving = false; if ( onChangeListener!= null)onChangeListener.onStopTrackingTouch(this); setPressed(false); setSelected(false); break; case MotionEvent.ACTION_CANCEL: isMoving = false; super.onTouchEvent(event); setPressed(false); setSelected(false); break; } return true; } public synchronized void setProgressAndThumb(int progress) { setProgress(progress); onSizeChanged(getWidth(), getHeight() , 0, 0); if ( onChangeListener!= null) onChangeListener.onProgressChanged(this, progress, true); } @Override public synchronized void setProgress(int progress) { this.progress =progress; super.setProgress(progress); if ( onChangeListener !=null) onChangeListener.onProgressChanged(this, progress, true); } public synchronized void setMaximum(int maximum) { setMax(maximum); } public synchronized int getMaximum() { return getMax(); } } 

问候!

 // I solved this problem. private int mOriginProgress; private boolean mIsDrag; @Override public void onStartTrackingTouch(SeekBar seekBar) { mIsTouched = true; mOriginProgress = seekBar.getProgress(); } @Override public void onProgressChanged(SeekBar seekBar, int p, boolean fromUser) { if (mOriginProgress == p) { return; } if (mIsDrag) { // do something } else { if (Math.abs(mOriginProgress - p) > 5) { seekBar.setProgress(mOriginProgress); } else { mIsDrag = true; } } } @Override public void onStopTrackingTouch(SeekBar seekBar) { mIsDrag = false; }