我有一个搜索栏,我想只允许用拇指/手柄部分进行更改。如果用户点击其他任何地方,我希望忽略该点击。
这是否有一个不安全的财产?如果没有,我已经知道我可以设置onTouch监听器并返回true
以“禁用”它,但是有没有办法检测拇指被点击的时间,以便我可以告诉onTouch返回false
在那种情况下?
答案 0 :(得分:3)
我假设你想通过代码更改拇指位置,这是用户限制的。所以试试这个。可以帮助你。
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);
}
}
});
答案 1 :(得分:0)
我遇到了同样的问题,我使用一个简单的逻辑来比较新的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();
}
}
问候!
答案 2 :(得分:0)
$ snap_screen mega_screen_capture
11272 KB/s (256237 bytes in 0.022s)
Saved to /Users/worker8/desktop/mega_screen_capture.png