SeekBar 的基本應用:
1.xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="3"
>
<SeekBar
android:id="@+id/seekbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#ff0000"
/>
</LinearLayout>
2.Activity
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends Activity {
private SeekBar seekbar=null;
private TextView text=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.seekbar=(SeekBar) super.findViewById(R.id.seekbar);
this.text=(TextView) super.findViewById(R.id.text);
//設置文本可以滾動
this.text.setMovementMethod(ScrollingMovementMethod.getInstance());
this.seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListenerImp());
}
private class OnSeekBarChangeListenerImp implements SeekBar.OnSeekBarChangeListener{
//觸發操作,拖動
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
MainActivity.this.text.append("正在拖動,當前值:"+seekBar.getProgress()+"\n");
}
//表示進度條剛開始拖動,開始拖動時候觸發的操作
public void onStartTrackingTouch(SeekBar seekBar) {
MainActivity.this.text.append("開始拖動,當前值:"+seekBar.getProgress()+"\n");
}
//停止拖動時候
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
MainActivity.this.text.append("停止拖動,當前值:"+seekBar.getProgress()+"\n");
}
}
}
以上就是SeekBar的基本應用...
自定義SeekBar:
首先是圖片的準備:
bar_dn.png:
bar_up.png:
thumb_dn.png:
thumb_up.png:
1.在res/drawable , 創建一個bg_bar.xml , 作爲SeekBar的進度條的背景.
<?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" />
<!-- 第二進度圖 -->
<item android:id="@+android:id/SecondaryProgress" android:drawable="@drawable/bar_dn" />
<!-- 進度度 -->
<item android:id="@+android:id/progress" android:drawable="@drawable/bar_up" />
</layer-list>
2.在res/drawable,創建一個thumb_bar.xml , 作爲SeekBar的拖動按鈕 .
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 按下狀態 -->
<item android:state_pressed="true"
android:drawable="@drawable/thumb_dn" />
<!-- 焦點狀態 -->
<item android:state_focused="true"
android:drawable="@drawable/thumb_up" />
<!-- 默認狀態 -->
<item android:drawable="@drawable/thumb_up" />
</selector>
3. 接下來就是layout佈局文件了,seek_bar_test.xml:
<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個值;
progress=10,代表默認值是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個值;
progress=20,代表默認值是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>
4. Activity中的應用:
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";
// 與“系統默認SeekBar”對應的TextView
private TextView mTvDef;
// 與“自定義SeekBar”對應的TextView
private TextView mTvSelf;
// “系統默認SeekBar”
private SeekBar mSeekBarDef;
// “自定義SeekBar”
private SeekBar mSeekBarSelf;
@Override
protected void onCreate(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
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:{
// 設置“與系統默認SeekBar對應的TextView”的值
mTvDef.setText(getResources().getString(R.string.text_def)+" : "+String.valueOf(seekBar.getProgress()));
break;
}
case R.id.seekbar_self: {
// 設置“與自定義SeekBar對應的TextView”的值
mTvSelf.setText(getResources().getString(R.string.text_self)+" : "+String.valueOf(seekBar.getProgress()));
break;
}
default:
break;
}
}
}