其他分享
首页 > 其他分享> > Android 控件 Seekba如何禁止拖动

Android 控件 Seekba如何禁止拖动

作者:互联网

1、创建一个ForbidDragSeekBar 类继承SeekBar ,当我们触摸到该控件的时候,在onTouchEvent方法中就返回false,该方法并未处理完全,该事件仍然需要以某种方式传递下去继续等待处理。

public class ForbidDragSeekBar extends SeekBar {

    /**
     * 是否支持拖动进度
     */
    private boolean touch = false;

    public ForbidDragSeekBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ForbidDragSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected synchronized void onDraw(Canvas canvas) {
//        LogUtil.getLog().d("voice progressbar onDraw");
        super.onDraw(canvas);

    }

    public void setTouch(boolean touch) {
        this.touch = touch;
    }

    /**
     * onTouchEvent 是在 SeekBar 继承的抽象类 AbsSeekBar
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (touch) {
            return super.onTouchEvent(event);
        }
        return false;
    }
}

2、在布局文件中使用

  <com.zqc.ui.view.ForbidDragSeekBar
        android:id="@+id/iv_music_progress"
        android:layout_width="380dp"
        android:layout_marginLeft="110dp"
        android:maxHeight="9dp"
        android:minHeight="9dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/size_30_dp"
        android:progress="100"
        android:thumbOffset="0dip"
        android:thumb="@drawable/shape_point_circular_t50"
        android:progressDrawable="@drawable/seekbar_bg" />

标签:控件,onTouchEvent,ForbidDragSeekBar,attrs,context,touch,Seekba,Android,public
来源: https://blog.csdn.net/wq892373445/article/details/120745651