如何让固定比例的垂直滑块在Android中工作?
我试图让垂直滑动条在0-50范围内工作,当用户移动滑动条时,我希望比例上的数字显示在文本视图中(滑块移动时实时更新) 环顾四周后,我已经使用中的代码实现了幻灯片 我是android新手,不知道如何从滑块获取值 我的代码如下: VerticalSlideBar.java如何让固定比例的垂直滑块在Android中工作?,android,slider,Android,Slider,我试图让垂直滑动条在0-50范围内工作,当用户移动滑动条时,我希望比例上的数字显示在文本视图中(滑块移动时实时更新) 环顾四周后,我已经使用中的代码实现了幻灯片 我是android新手,不知道如何从滑块获取值 我的代码如下: VerticalSlideBar.java import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.vi
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" />
这就是我达到极限的地方。我需要帮助设置滑动条的固定范围,将值实时更新为int
感谢您提供的帮助。我发现我不需要固定刻度,我正在使用以下方法读取垂直滑块的值:
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");
}
}
希望这个答案对将来的人有所帮助 我发现我不需要固定的刻度,我使用以下方法读取垂直滑块的值:
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");
}
}
希望这个答案对将来的人有所帮助 有什么我可以添加到这篇文章中,让它变得更清晰并得到答案吗?有什么我可以添加到这篇文章中,让它更清晰并得到答案吗?