您的位置:首页 > 移动开发 > Android开发

Android控件之SeekBar 设置最小值非零

2016-06-28 15:54 603 查看
SeekBar的最小值为0,但是在开发中很多需求是不从0开始,SeekBar也并没有提供设置最小值的方法,那么要如何自己设置最小值呢,下面请看我的代码,我的范围是最小值为16最大值为26.

Layout文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <EditText 

        android:id="@+id/et_num"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content" />

    <SeekBar

        android:id="@+id/seekBar"

        android:layout_marginTop="100dp"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:max="10" />

</LinearLayout>

应用层代码:

public class MainActivity extends Activity implements OnSeekBarChangeListener{

private EditText num;
private SeekBar seekBar;
private int minNum = 16;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
num.setText(minNum+"");
}

public void initView()
{
num = (EditText) findViewById(R.id.et_num);
seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(this);
}

// 滚动时
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
minNum = 16 + progress;
num.setText(minNum+"");
}

// 开始滚动
@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

// 停止滚动
@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}

}

效果图:




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: