我有一個seekbar,我想只允許更改拇指/手柄部分。如果用戶點擊其他任何地方,我希望該點擊被忽略。Android Seekbar - 只允許用拇指進行更改?
有沒有一個不容忽視的財產做到這一點?如果不是,我已經知道我可以設置一個onTouch監聽器並返回true
來「禁用」它,但是有沒有辦法檢測拇指被輕敲的時間,以便我可以告訴onTouch在這種情況下返回false
?
我有一個seekbar,我想只允許更改拇指/手柄部分。如果用戶點擊其他任何地方,我希望該點擊被忽略。Android Seekbar - 只允許用拇指進行更改?
有沒有一個不容忽視的財產做到這一點?如果不是,我已經知道我可以設置一個onTouch監聽器並返回true
來「禁用」它,但是有沒有辦法檢測拇指被輕敲的時間,以便我可以告訴onTouch在這種情況下返回false
?
我假設你想通過代碼來改變拇指位置,這是用戶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;
}
我也想知道這個問題的答案... – Josh
您可以在這裏看到我的回答 http://stackoverflow.com/questions/5126091/disable-progress-change-when-用戶單抽頭 - 上的-搜索條功能於機器人/ 16992969#16992969 – Slim