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) {
}
}
效果图:
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) {
}
}
效果图:
相关文章推荐
- Android提高Service优先级的方法分析
- android EventBus 3.0 混淆配置
- 如何在onCreate里面绘制View宽高
- Android 自定义RecyclerView 实现真正的Gallery效果
- Android——操作摄像头、图片合成
- android GPS 定位
- Android中使用Canvas绘制简单的图形(一)
- Android中persistent属性用法详解
- Android vold启下篇(NetlinkHandler)
- Android BottomSheet:以选取图片为例(2)
- Android BottomSheet:以选取图片为例(2)
- Android自定义ScrollView实现一键置顶功能
- Android studio 提高导入项目的速度
- Android开发技巧:像QQ一样输入表情图像
- Android 下拉展开动画
- android(七)、 ContextImpl创建
- AndroidStudio快捷键大全
- 如何将Android Studio项目提交(更新)到github作为远程仓库
- Android 适配不同的屏幕
- android+studio引用library与导入jar