Android控件之SeekBar的用法總結(jié)
1 SeekBar簡介
SeekBar是進(jìn)度條。我們使用進(jìn)度條時(shí),可以使用系統(tǒng)默認(rèn)的進(jìn)度條;也可以自定義進(jìn)度條的圖片和滑塊圖片等。
2 SeekBar示例
創(chuàng)建一個(gè)activity,包含2個(gè)SeekBar。
第1個(gè)SeekBar是系統(tǒng)默認(rèn)的SeekBar。
第2個(gè)SeekBar是自定義SeekBar,使用自定義的背景圖和滑塊圖片。
應(yīng)用層代碼
package com.skywang.control;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.widget.TextView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class SeekBarTest extends Activity implements SeekBar.OnSeekBarChangeListener{
private static final String TAG = "SKYWANG";
// 與“系統(tǒng)默認(rèn)SeekBar”對應(yīng)的TextView
private TextView mTvDef;
// 與“自定義SeekBar”對應(yīng)的TextView
private TextView mTvSelf;
// “系統(tǒng)默認(rèn)SeekBar”
private SeekBar mSeekBarDef;
// “自定義SeekBar”
private SeekBar mSeekBarSelf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seek_bar_test);
// 與“系統(tǒng)默認(rèn)SeekBar”對應(yīng)的TextView
mTvDef = (TextView) findViewById(R.id.tv_def);
// “系統(tǒng)默認(rèn)SeekBar”
mSeekBarDef = (SeekBar) findViewById(R.id.seekbar_def);
mSeekBarDef.setOnSeekBarChangeListener(this);
// 與“自定義SeekBar”對應(yīng)的TextView
mTvSelf = (TextView) findViewById(R.id.tv_self);
// “自定義SeekBar”
mSeekBarSelf = (SeekBar) findViewById(R.id.seekbar_self);
mSeekBarSelf.setOnSeekBarChangeListener(this);
}
/*
* SeekBar停止?jié)L動(dòng)的回調(diào)函數(shù)
*/
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
/*
* SeekBar開始滾動(dòng)的回調(diào)函數(shù)
*/
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
/*
* SeekBar滾動(dòng)時(shí)的回調(diào)函數(shù)
*/
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
Log.d(TAG, "seekid:"+seekBar.getId()+", progess"+progress);
switch(seekBar.getId()) {
case R.id.seekbar_def:{
// 設(shè)置“與系統(tǒng)默認(rèn)SeekBar對應(yīng)的TextView”的值
mTvDef.setText(getResources().getString(R.string.text_def)+" : "+String.valueOf(seekBar.getProgress()));
break;
}
case R.id.seekbar_self: {
// 設(shè)置“與自定義SeekBar對應(yīng)的TextView”的值
mTvSelf.setText(getResources().getString(R.string.text_self)+" : "+String.valueOf(seekBar.getProgress()));
break;
}
default:
break;
}
}
}
代碼說明:
要監(jiān)聽SeekBar的滑動(dòng)消息,通過實(shí)現(xiàn)“SeekBar.OnSeekBarChangeListener”接口。這個(gè)接口中包含3個(gè)方法onStartTrackingTouch()、onStopTrackingTouch()和onProgressChanged()。
layout文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_def"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_def" />
<!--
max=100,代表它的取值范圍是0-100,共101個(gè)值;
progress=10,代表默認(rèn)值是10
-->
<SeekBar
android:id="@+id/seekbar_def"
android:layout_width="620px"
android:layout_height="wrap_content"
android:max="100"
android:progress="10"
/>
<TextView
android:id="@+id/tv_self"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_self" />
<!--
max=100,代表它的取值范圍是0-100,共101個(gè)值;
progress=20,代表默認(rèn)值是20
progressDrawable,表示SeekBar的背景圖片
thumbe,表示SeekBar的滑塊圖片
-->
<SeekBar
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" />
</LinearLayout>
自定義SeekBar的背景定義為:android:progressDrawable="@drawable/bg_bar"。
它調(diào)用的bg_bar.xml的內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 背景圖 --> <item android:id="@+android:id/background" android:drawable="@drawable/bar_dn" /> <!-- 第二進(jìn)度圖 --> <item android:id="@+android:id/SecondaryProgress" android:drawable="@drawable/bar_dn" /> <!-- 進(jìn)度度 --> <item android:id="@+android:id/progress" android:drawable="@drawable/bar_up" /> </layer-list>
bar_dn.png如下圖:

bar_up.png如下圖:

自定義SeekBar的滑塊定義為:android:thumb="@drawable/thumb_bar"。
它調(diào)用的thumb_bar.xml的內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 按下狀態(tài) -->
<item android:state_pressed="true"
android:drawable="@drawable/thumb_dn" />
<!-- 焦點(diǎn)狀態(tài) -->
<item android:state_focused="true"
android:drawable="@drawable/thumb_up" />
<!-- 默認(rèn)狀態(tài) -->
<item android:drawable="@drawable/thumb_up" />
</selector>
thumb_up.png如下圖:

thumb_dn.png如下圖:

manifest文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.skywang.control"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.skywang.control.SeekBarTest"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
點(diǎn)擊下載:源代碼
運(yùn)行效果:如圖

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 可拖動(dòng)的seekbar自定義進(jìn)度值
- Android利用SeekBar實(shí)現(xiàn)簡單音樂播放器
- Android 中Seekbar詳解及簡單實(shí)例
- Android自定義SeekBar實(shí)現(xiàn)視頻播放進(jìn)度條
- Android控件SeekBar仿淘寶滑動(dòng)驗(yàn)證效果
- Android 動(dòng)態(tài)改變SeekBar進(jìn)度條顏色與滑塊顏色的實(shí)例代碼
- Android自定義豎直方向SeekBar多色進(jìn)度條
- android之SeekBar控件用法詳解
- Android自定義SeekBar滑動(dòng)顯示數(shù)字
- Android關(guān)于SeekBar無法點(diǎn)擊到最大值問題解決方法記錄(推薦)
相關(guān)文章
Android 實(shí)現(xiàn)切圓圖作為頭像使用實(shí)例
這篇文章主要介紹了Android 實(shí)現(xiàn)切圓圖作為頭像使用實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-12-12
Android開發(fā)之ListView實(shí)現(xiàn)Item局部刷新
對于ListView數(shù)據(jù)的刷新大家都知道,改變Adapter的數(shù)據(jù)源,然后調(diào)用Adapter的notifyDateSetChanged()方法即可。通過本篇文章給大家詳細(xì)介紹Android開發(fā)之ListView實(shí)現(xiàn)Item局部刷新,感興趣的朋友一起學(xué)習(xí)吧2015-10-10
Android 日志系統(tǒng)Logger源代碼詳細(xì)介紹
本文主要介紹Android 日志系統(tǒng)Logger,這里整理了關(guān)于Android源碼的日志系統(tǒng)資料,有研究Android源碼的朋友可以參考下2016-08-08
flutter日期選擇器 flutter時(shí)間選擇器
這篇文章主要為大家詳細(xì)介紹了flutter日期選擇器,flutter時(shí)間選擇器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Android Service服務(wù)不被停止詳解及實(shí)現(xiàn)
這篇文章主要介紹了Android Service服務(wù)不被停止詳解及實(shí)現(xiàn)的相關(guān)資料,有很多應(yīng)用在設(shè)置運(yùn)行中會(huì)被直接停止掉,這里就提供一個(gè)方法一直運(yùn)行,需要的朋友可以參考下2016-11-11
Android完美實(shí)現(xiàn)平滑過渡的ViewPager廣告條
這篇文章主要為大家詳細(xì)介紹了Android完美實(shí)現(xiàn)平滑過渡的ViewPager廣告條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android開發(fā)新手必須知道的10大嚴(yán)重錯(cuò)誤
這篇文章主要介紹了Android開發(fā)新手必須知道的10大嚴(yán)重錯(cuò)誤,總結(jié)分析了Android開發(fā)中幫助文件、開發(fā)工具、社區(qū)等的重要性以及重要的開發(fā)原則,需要的朋友可以參考下2016-01-01

