最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Android控件中SeekBar的用法总结
时间:2017-01-05 编辑:简简单单 来源:一聚教程网
1 SeekBar简介
SeekBar是进度条。我们使用进度条时,可以使用系统默认的进度条;也可以自定义进度条的图片和滑块图片等。
2 SeekBar示例
创建一个activity,包含2个SeekBar。
第1个SeekBar是系统默认的SeekBar。
第2个SeekBar是自定义SeekBar,使用自定义的背景图和滑块图片。
应用层代码
代码如下 | 复制代码 |
packagecom.skywang.control;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.util.Log;
importandroid.widget.TextView;
importandroid.widget.SeekBar;
importandroid.widget.SeekBar.OnSeekBarChangeListener;
publicclassSeekBarTestextendsActivityimplementsSeekBar.OnSeekBarChangeListener{
privatestaticfinalString TAG ="SKYWANG";
// 与“系统默认SeekBar”对应的TextView
privateTextView mTvDef;
// 与“自定义SeekBar”对应的TextView
privateTextView mTvSelf;
// “系统默认SeekBar”
privateSeekBar mSeekBarDef;
// “自定义SeekBar”
privateSeekBar mSeekBarSelf;
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seek_bar_test);
// 与“系统默认SeekBar”对应的TextView
mTvDef = (TextView) findViewById(R.id.tv_def);
// “系统默认SeekBar”
mSeekBarDef = (SeekBar) findViewById(R.id.seekbar_def);
mSeekBarDef.setOnSeekBarChangeListener(this);
// 与“自定义SeekBar”对应的TextView
mTvSelf = (TextView) findViewById(R.id.tv_self);
// “自定义SeekBar”
mSeekBarSelf = (SeekBar) findViewById(R.id.seekbar_self);
mSeekBarSelf.setOnSeekBarChangeListener(this);
}
/*
* SeekBar停止滚动的回调函数
*/
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
/*
* SeekBar开始滚动的回调函数
*/
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
/*
* SeekBar滚动时的回调函数
*/
@Override
publicvoidonProgressChanged(SeekBar seekBar,intprogress,
booleanfromUser) {
Log.d(TAG,"seekid:"+seekBar.getId()+", progess"+progress);
switch(seekBar.getId()) {
caseR.id.seekbar_def:{
// 设置“与系统默认SeekBar对应的TextView”的值
mTvDef.setText(getResources().getString(R.string.text_def)+" : "+String.valueOf(seekBar.getProgress()));
break;
}
caseR.id.seekbar_self: {
// 设置“与自定义SeekBar对应的TextView”的值
mTvSelf.setText(getResources().getString(R.string.text_self)+" : "+String.valueOf(seekBar.getProgress()));
break;
}
default:
break;
}
}
}
|
代码说明:
要监听SeekBar的滑动消息,通过实现“SeekBar.OnSeekBarChangeListener”接口。这个接口中包含3个方法onStartTrackingTouch()、onStopTrackingTouch()和onProgressChanged()。
layout文件
代码如下 | 复制代码 |
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:id="@+id/tv_def"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_def"/>
android:id="@+id/seekbar_def"
android:layout_width="620px"
android:layout_height="wrap_content"
android:max="100"
android:progress="10"
/>
android:id="@+id/tv_self"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_self"/>
android:id="@+id/seekbar_self"
android:layout_width="620px"
android:layout_height="wrap_content"
android:max="100"
android:progress="20"
android:progressDrawable="@drawable/bg_bar"
android:thumb="@drawable/thumb_bar"/>
|
自定义SeekBar的背景定义为:android:progressDrawable="@drawable/bg_bar"。
它调用的bg_bar.xml的内容如下:
代码如下 | 复制代码 |
|
bar_dn.png如下图:
bar_up.png如下图:
自定义SeekBar的滑块定义为:android:thumb="@drawable/thumb_bar"。
它调用的thumb_bar.xml的内容如下:
代码如下 | 复制代码 |
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
android:drawable="@drawable/thumb_dn"/>
android:drawable="@drawable/thumb_up"/>
|
thumb_up.png如下图:
thumb_dn.png如下图:
manifest文件
代码如下 | 复制代码 |
package="com.skywang.control"
android:versionCode="1"
android:versionName="1.0">
android:minSdkVersion="8"
android:targetSdkVersion="17"/>
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
android:name="com.skywang.control.SeekBarTest"
android:label="@string/app_name">
|