1

我试图让垂直滑动条在 0-50 的范围内工作,当用户移动滑动条时,我希望刻度上的数字显示在文本视图中(滑块移动时实时更新)。

环顾四周后,我使用此处的代码实现了滑动条。

我是 android 新手,不知道如何从滑块中获取值?

我的代码如下:

VerticalSlideBar.java

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;

public class VerticalSeekBar extends SeekBar {

    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 boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
                setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
                onSizeChanged(getWidth(), getHeight(), 0, 0);
                break;

            case MotionEvent.ACTION_CANCEL:
                break;
        }
        return true;
    }
}

main.xml 文件是:

     <com.test.VerticalSeekBar
        android:id="@+id/seekBar1"
        android:layout_width="wrap_content"
        android:layout_height="440dp"
        android:layout_alignBottom="@+id/marT1"
        android:layout_marginRight="120dp"
        android:layout_toLeftOf="@+id/switch1" />

main.java 是我感到困惑的领域,我实现了以下内容:

private SeekBar seekBar1;
seekBar1 = (SeekBar)findViewById(R.id.seekBar1);

这就是我达到极限的地方。我需要帮助设置滑动条的固定范围,将值实时更新为 int。

感谢您提供的任何帮助。

4

1 回答 1

1

我发现我不需要固定比例,我正在使用以下内容来读取垂直滑块的值:

public class TestSlider extends Activity implements OnSeekBarChangeListener{
    private SeekBar bar; // declare seekbar object variable
    // declare text label objects
    private TextView textProgress,textAction;

 @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // load the layout
        setContentView(R.layout.main);     
        bar = (SeekBar)findViewById(R.id.seekBar1); // make seekbar object
        bar.setOnSeekBarChangeListener(this); // set seekbar listener.

        // make text label for progress value
        textProgress = (TextView)findViewById(R.id.textViewProgress);
        // make text label for action
        textAction = (TextView)findViewById(R.id.textViewAction);

    }


public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub

        // change progress text label with current seekbar value
        textProgress.setText("The value is: "+progress);
        // change action text label to changing
        textAction.setText("changing");
    }

public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
        textAction.setText("starting to track touch");

    }

 public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
        seekBar.setSecondaryProgress(seekBar.getProgress());
        textAction.setText("ended tracking touch");     
    }

}

希望这个答案对将来的某人有所帮助。

于 2013-09-09T18:20:45.403 回答