我们在千千静听中都会遇到进度条的效果,进度条可以给用户提供良好的体验,Android系统已经为我们提供了ProgressBar类来完成进度条的效果,我们可以很方便的运用该类。其中常见的进度条有两中分别是“水平进度条”和“环形进度条”,在布局文件中定义两种进度条的方式比较相似,区别是,定义“水平进度条”时需要加上一项属性“style="?android:attr/progressBarStyleHorizontal"”。
ProgressBar类中常用的方法如下:
ProgressBar.setMax(intmax);设置总长度为100
ProgressBar.setProgress(intprogress);设置已经开启长度为0,假设设置为50,进度条将进行到一半停止。
下面是一个应用实例:
首先在layout文件中建一个progressbarz_layout.xml文件,代码如下:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="进度条演示"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="1000"
android:progress="100"
android:id="@+id/progressbar"
/>
<ProgressBar
style="@android:style/Widget.ProgressBar.Horizontal"//该行指定该ProgressBar为水平样式。
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="1000"
android:progress="100"
android:secondaryProgress="300"
android:id="@+id/progressbar2"
/>
</LinearLayout>
效果图如下:
拖动条(SeekBar):
SeekBar.getProgress():获取拖动条当前值
调用setOnSeekBarChangeListener()方法,处理拖动条值变化事件,把SeekBar.OnSeekBarChangeListener实例作为参数传入
下面的实例可以模拟实现该进度(拖动)条,还可以通过拖动游标改变进度值,每次拖动之后仍会自动更新。
新建一个seekbar_layout.xml文件:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="1000"
android:id="@+id/seekbar"
/>
</LinearLayout>
在java文件中新建一个SeekBarDemo文件:
packagecn.class3g.activity;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.widget.SeekBar;
importandroid.widget.SeekBar.OnSeekBarChangeListener;
PublicclassSeekBarDemoextendsActivityimplementsOnSeekBarChangeListener{
SeekBarseekbar=null;
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.seekbar_layout);
findViews();
}
privatevoidfindViews(){
seekbar=(SeekBar)this.findViewById(R.id.seekbar);
seekbar.setOnSeekBarChangeListener(this);
}
publicvoidonProgressChanged(SeekBarseekBar,intprogress,
booleanfromUser){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidonStartTrackingTouch(SeekBarseekBar){
Log.d("TAG","start:"+String.valueOf(seekBar.getProgress()));
}
@Override
publicvoidonStopTrackingTouch(SeekBarseekBar){
Log.d("TAG","start:"+String.valueOf(seekBar.getProgress()));
}
}
显示的效果图为: