Android控件详解之进度控件

来源:互联网 发布:古筝曲谱制作软件 编辑:程序博客网 时间:2024/05/18 02:05

我将Android控件的进度控件的学习知识总结一下和大家共享包括(ProgressBar、SeekBar、RatingBar)

在Android开发中,任务或者工作进度就是需要用到进度控件,Android源生提供了progressbar、seekbar、ratingbar这三种进度控件。

1、ProgressBar控件

ProgressBar控件在默认的情况下是圆形的进度条,可通过style属性将圆形进度条设为大、中、小3种形式(引用系统style资源用?)

style="?android:attr/progressBarStyleSmallTitle"  //小的形式
//默认是中的大小形式,不设置即可
<pre name="code" class="html">style="?android:attr/progressBarStyleLargeTitle"  //大的形式 

ProgressBar还提供支持水平进度条

style="?android:attr/progressBarStyleHorizontal" android:max="100"android:progress="30" android:secondaryProgress="60"
水平进度条支持两级进度android:progress和android:secondaryProgress属性设置。android:max为最大刻度值。

另外Android系统还支持将水平和圆形进度条放在窗口的标题栏上,

requestWindowFeature(Window.FEATURE_PROGRESS);requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);setContentView(R.layout.main);setProgressBarVisibility(true);     //  显示水平进度条setProgressBarIndeterminateVisibility(true);    //    显示圆形进度条setProgress(3500);<span style="white-space:pre"></span>//显示水平进度条当前的进度
注意:requestWindowFeature方法应在调用setContentView方法之前,否则系统会抛出异常。setProgressBarVisibility、setProgressBarIndeterminateVisibility、setProgress

方法必须在setContentView方法之后,否则会无效。标题栏上默认最大刻度是10000。

下面是一个实例:

布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="圆形进度条(小)" /><ProgressBar android:layout_width="wrap_content"android:layout_height="wrap_content" style="?android:attr/progressBarStyleSmallTitle" /><TextView android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="圆形进度条(中)" /><ProgressBar android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextView android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="圆形进度条(大)" /><ProgressBar android:layout_width="wrap_content"android:layout_height="wrap_content" style="?android:attr/progressBarStyleLarge" /><TextView android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="水平进度条" /><ProgressBar android:layout_width="fill_parent"android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal"android:max="100" android:progress="30" /><ProgressBar android:id="@+id/progressBarHorizontal"android:layout_width="fill_parent" android:layout_height="wrap_content"style="?android:attr/progressBarStyleHorizontal" android:max="100"android:progress="30" android:secondaryProgress="60"android:layout_marginTop="20dp" /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal" android:layout_width="fill_parent"android:layout_height="fill_parent"><Button android:id="@+id/button1" android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_marginTop="20dp"android:text="增加进度" /><Button android:id="@+id/button2" android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_marginTop="20dp"android:text="减小进度" /></LinearLayout></LinearLayout>
java代码文件:

public class Main extends Activity implements OnClickListener{private ProgressBar progressBarHorizontal;@Overridepublic void onClick(View view){switch (view.getId()){// 处理“增加进度"按钮事件case R.id.button1:progressBarHorizontal.setProgress((int) (progressBarHorizontal.getProgress() * 1.2));progressBarHorizontal.setSecondaryProgress((int) (progressBarHorizontal.getSecondaryProgress() * 1.2));break;// 处理“减少进度"按钮事件case R.id.button2:progressBarHorizontal.setProgress((int) (progressBarHorizontal.getProgress() * 0.8));progressBarHorizontal.setSecondaryProgress((int) (progressBarHorizontal.getSecondaryProgress() * 0.8));break;}}@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_PROGRESS);requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);setContentView(R.layout.main);setProgressBarVisibility(true);setProgressBarIndeterminateVisibility(true);setProgress(3500);progressBarHorizontal = (ProgressBar) findViewById(R.id.progressBarHorizontal);Button button1 = (Button) findViewById(R.id.button1);Button button2 = (Button) findViewById(R.id.button2);button1.setOnClickListener(this);button2.setOnClickListener(this);}}
2、SeekBar控件

SeekBar控件是可以通过拖动滑杆改变当前的值,SeekBar其实是ProgressBar的子类,但是它一般不设置第2进度。

与SeekBar控件滑动相关事件接口是OnSeekBarChangeListener,该接口定义了如下3个事件:

public void onStartTrackingTouch(SeekBar seekBar)

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)

public void onStopTrackingTouch(SeekBar seekBar)

当按住滑杆后,系统会调用onStartTrackingTouch方法,在滑杆滑动时,会调用onProgressChanged方法,松开会调用onStopTrackingTouch

下面一个例子:

布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:id="@+id/textview1" android:layout_width="fill_parent"android:layout_height="wrap_content" /><TextView android:id="@+id/textview2" android:layout_width="fill_parent"android:layout_height="wrap_content" /><SeekBar android:id="@+id/seekbar1" android:layout_width="fill_parent"android:layout_height="wrap_content" android:max="100"android:progress="30" /><SeekBar android:id="@+id/seekbar2" android:layout_width="fill_parent"android:layout_height="wrap_content" android:max="100"android:progress="30" android:secondaryProgress="60"/></LinearLayout>
java代码文件:

public class Main extends Activity implements OnSeekBarChangeListener{private TextView textView1;private TextView textView2;@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser){if (seekBar.getId() == R.id.seekbar1)textView2.setText("seekbar1的当前位置:" + progress);elsetextView2.setText("seekbar2的当前位置:" + progress);}@Overridepublic void onStartTrackingTouch(SeekBar seekBar){if (seekBar.getId() == R.id.seekbar1)textView1.setText("seekbar1开始拖动");elsetextView1.setText("seekbar2开始拖动");}@Overridepublic void onStopTrackingTouch(SeekBar seekBar){if (seekBar.getId() == R.id.seekbar1)textView1.setText("seekbar1停止拖动");elsetextView1.setText("seekbar2停止拖动");}@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);SeekBar seekBar1 = (SeekBar) findViewById(R.id.seekbar1);seekBar1.setOnSeekBarChangeListener(this);SeekBar seekBar2 = (SeekBar) findViewById(R.id.seekbar2);seekBar2.setOnSeekBarChangeListener(this);textView1 = (TextView) findViewById(R.id.textview1);textView2 = (TextView) findViewById(R.id.textview2);}}
3、设置ProgressBar和SeekBar控件的颜色和背景图

ProgressBar和seekbar控件默认都是黄色的,而且这两个类并未提供直接修改颜色或背景图的方法和属性。不过有另外一个属性android:progressDrawable

其实一个完整的ProgressBar或者SeekBar是由3部分组成的:1级进度、2级进度、背景

下面一个例子:

布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:layout_marginTop="5dp"android:text="barcolor1" /><ProgressBar android:id="@+id/progressBarHorizontal1"android:layout_width="fill_parent" android:layout_height="wrap_content"style="?android:attr/progressBarStyleHorizontal"android:max="100" android:progress="30" android:secondaryProgress="60"android:progressDrawable="@drawable/barcolor1" /><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:layout_marginTop="5dp"android:text="barcolor2" /><ProgressBar android:id="@+id/progressBarHorizontal2"android:layout_width="fill_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal"android:max="100" android:progress="30" android:secondaryProgress="60"android:progressDrawable="@drawable/barcolor2" /><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:layout_marginTop="5dp"android:text="barcolor1" /><SeekBar android:layout_width="fill_parent"android:layout_height="wrap_content" android:max="100" android:progress="30"android:progressDrawable="@drawable/barcolor1" /><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:layout_marginTop="5dp"android:text="barcolor2" /><SeekBar android:layout_width="fill_parent"android:layout_height="wrap_content" android:max="100" android:progress="30"android:progressDrawable="@drawable/barcolor2" /><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:layout_marginTop="5dp"android:text="Face拖动条" /><SeekBar android:layout_width="fill_parent"android:layout_height="wrap_content" android:max="100"android:progress="30" android:progressDrawable="@drawable/barface" /></LinearLayout>
barface.xml

<?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/bg" /><item android:id="@android:id/progress" android:drawable="@drawable/face" /></layer-list>                    
barcolor1.xml

<?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/bg" /><item android:id="@android:id/secondaryProgress" android:drawable="@drawable/secondary"  /><item android:id="@android:id/progress" android:drawable="@drawable/progress" /></layer-list>              
barcolor2.xml

<?xml version="1.0" encoding="UTF-8"?><layer-list  xmlns:android="http://schemas.android.com/apk/res/android"><item android:id="@android:id/background"><shape><corners android:radius="10dip" /><gradient android:startColor="#FFFF0000"android:centerColor="#FF880000" android:centerY="0.75"android:endColor="#FF110000" android:angle="270" /></shape></item><item android:id="@android:id/secondaryProgress"><clip><shape><corners android:radius="10dp" /><gradient android:startColor="#FF00FF00"android:centerColor="#FF00FF00" android:centerY="0.75"android:endColor="#FF00FF00" android:angle="270" /></shape></clip></item><item android:id="@android:id/progress"><clip><shape><corners android:radius="10dp" /><gradient android:startColor="#ffffd300"android:centerColor="#ffffb600" android:centerY="0.75"android:endColor="#ffffcb00" android:angle="270" /></shape></clip></item></layer-list>
4、RatingBar控件

RatingBar是一个评分控件,有3个常用属性:

android:numStars 用于指定评分的五角星数

android:rating 用于指定当前分数

android:stepSize 用于指定分数的增量单位,默认是0.5

OnRatingBarChangeListener接口可以监听RatingBar分数的变化,在OnRatingChanged方法处理分数变化。

下面一个例子:

布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="3颗星,步长是0.5" /><RatingBar android:id="@+id/ratingbar1" android:layout_width="wrap_content"android:layout_height="wrap_content" android:numStars="3" android:rating="2" /><TextView android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="5颗星,步长是0.1" /><RatingBar android:id="@+id/ratingbar2" android:layout_width="wrap_content"android:layout_height="wrap_content" android:numStars="5"android:stepSize="0.1" /><LinearLayout android:layout_width="fill_parent"android:layout_height="wrap_content" android:layout_marginTop="10dip"><TextView android:id="@+id/textview" android:layout_width="wrap_content"android:layout_height="wrap_content"  /><RatingBar android:id="@+id/smallRatingbar" style="?android:attr/ratingBarStyleSmall"android:layout_marginLeft="5dip" android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout><RatingBar android:id="@+id/indicatorRatingbar" style="?android:attr/ratingBarStyleIndicator"android:layout_marginLeft="5dip" android:layout_width="wrap_content"android:layout_height="wrap_content" android:stepSize="0.1" /></LinearLayout>
java文件:

public class Main extends Activity implements OnRatingBarChangeListener{private RatingBar smallRatingBar;private RatingBar indicatorRatingBar;private TextView textView;@Overridepublic void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser){smallRatingBar.setRating(rating);indicatorRatingBar.setRating(rating);if (ratingBar.getId() == R.id.ratingbar1)textView.setText("ratingbar1的分数:" + rating);elsetextView.setText("ratingbar2的分数:" + rating);}@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);RatingBar ratingBar1 = (RatingBar) findViewById(R.id.ratingbar1);RatingBar ratingBar2 = (RatingBar) findViewById(R.id.ratingbar2);textView = (TextView) findViewById(R.id.textview);ratingBar1.setOnRatingBarChangeListener(this);ratingBar2.setOnRatingBarChangeListener(this);smallRatingBar = (RatingBar) findViewById(R.id.smallRatingbar);indicatorRatingBar = (RatingBar) findViewById(R.id.indicatorRatingbar);}}




0 0
原创粉丝点击