十一、ProgressBar 进度条
在某项延续性工作的进展过程中为了不让用户觉得程序死掉了,需要有个活动的进度条,表示此过程正在进行中。Android中使用ProgressBar来实现这一功能:
1、简单的进度条
在xml中添加:
  1. <ProgressBar android:id=”@+id/ProgressBar01″

  2. android:layout_width=”wrap_content”

  3. android:layout_height=”wrap_content”>

  4. </ProgressBar>

复制代码

就可以看到下面,圆形的、大大的圈圈:
2、各种各样的圆圈:
注意标题栏上也有一个进度条,下面是代码:
  1. package android.basic.lesson11;


  2. import android.app.Activity;

  3. import android.os.Bundle;

  4. import android.view.Window;


  5. public class MainHelloProgressBar extends Activity {

  6.    /** Called when the activity is first created. */

  7.    @Override

  8.    public void onCreate(Bundle savedInstanceState) {

  9.        super.onCreate(savedInstanceState);

  10.      //在标题条里放置进度条。请求窗口特色风格,这里设置成不明确的进度风格

  11.      requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

  12.      setContentView(R.layout.main);

  13.      //设置标题栏中的不明确的进度条是否可以显示,当你需要表示处理中的时候设置为True,处理完毕后设置为false

  14.      setProgressBarIndeterminateVisibility(true);

  15.    }

  16. }

复制代码

下面试main.xml中的代码,大家注意黑体字部分的内容,看看不同样式的不同的表现:

  1. <?xml version=”1.0″ encoding=”utf-8″?><LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”android:orientation=”vertical”android:background=”#003399″android:layout_width=”fill_parent”android:layout_height=”fill_parent”><ProgressBar android:id=”@+id/ProgressBar01″android:layout_width=”wrap_content”android:layout_height=”wrap_content”></ProgressBar><ProgressBar android:id=”@+id/ProgressBar02″style=”?android:attr/progressBarStyleLarge”           大圆圈android:layout_width=”wrap_content”android:layout_height=”wrap_content”></ProgressBar><ProgressBar android:id=”@+id/ProgressBar03″style=”?android:attr/progressBarStyleSmall”          小圆圈android:layout_width=”wrap_content”android:layout_height=”wrap_content”></ProgressBar><ProgressBar android:id=”@+id/ProgressBar03″style=”?android:attr/progressBarStyleSmallTitle”   标题条的样式android:layout_width=”wrap_content”android:layout_height=”wrap_content”></ProgressBar>

  2. </LinearLayout>

复制代码


3、长条状的进度条
xml代码:

  1. <ProgressBar android:id=”@+id/ProgressBar04″style=”?android:attr/progressBarStyleHorizontal”android:layout_marginLeft=”10dp”

  2. android:layout_marginRight=”10dp”android:max=”100″                                  最大刻度按100算android:progress=”30″                              第一进度是30android:secondaryProgress=”80″              第二进度是80android:layout_width=”fill_parent”              进度条的显示长度是铺满父窗口android:layout_height=”wrap_content”></ProgressBar><?xml version=”1.0″ encoding=”utf-8″?><LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”android:orientation=”vertical”android:background=”#003399″android:layout_width=”fill_parent”android:layout_height=”fill_parent”><ProgressBar android:id=”@+id/ProgressBar04″style=”?android:attr/progressBarStyleHorizontal”android:layout_marginTop=”10dp”android:layout_marginLeft=”10dp”android:layout_marginRight=”10dp”android:max=”100″android:progress=”30″android:secondaryProgress=”80″android:layout_width=”fill_parent”android:layout_height=”wrap_content”></ProgressBar>

  3. </LinearLayout>


复制代码


  1. package android.basic.lesson11;


  2. import android.app.Activity;


  3. import android.os.Bundle;


  4. import android.view.Window;


  5. public class MainHelloProgressBar extends Activity {


  6. /** Called when the activity is first created. */


  7. @Override


  8. public void onCreate(Bundle savedInstanceState) {


  9. super.onCreate(savedInstanceState);


  10. //设置窗口进度条特性风格


  11. requestWindowFeature(Window.FEATURE_PROGRESS);


  12. setContentView(R.layout.main);


  13. //设置进度条可见性


  14. setProgressBarVisibility(true);


  15. //设置进度条进度值,要乘以100的


  16. setProgress(60*100);


  17. setSecondaryProgress(80*100);

  18. }


  19. }

复制代码



运行效果:
同学们可以使用下面的代码对,进度条进行一些操作练习:
private ProgressBar pb;                                                        //定义ProgressBar
pb = (ProgressBar) findViewById(R.id.ProgressBar01);
pb.incrementProgressBy(5);                                                 //ProgressBar进度值增加5
pb.incrementProgressBy(-5);                                                //ProgressBar进度值减少5
pb.incrementSecondaryProgressBy(5);                                 //ProgressBar第二个进度条 进度值增加5
pb.incrementSecondaryProgressBy(-5);                                //ProgressBar第二个进度条 进度值减少5
十二、SeekBar 拖动条 滑动条
SeekBar可以作为音乐播放器的进度指示和调整工具,音量调整工具等,SeekBar是ProgressBar的一个子类,下面我们用一个可以改变并显示当前进度的拖动条例子来演示一下它的使用:
1、main.xml
< ?xml version="1.0" encoding="utf-8"?>                                
2、java:
  1. package android.basic.lesson11;


  2. import android.app.Activity;

  3. import android.os.Bundle;

  4. import android.widget.SeekBar;

  5. import android.widget.SeekBar.OnSeekBarChangeListener;

  6. import android.widget.TextView;

  7. import android.widget.Toast;


  8. public class MainHelloSeekBar extends Activity {

  9.        /** Called when the activity is first created. */

  10.        @Override

  11.        public void onCreate(Bundle savedInstanceState) {

  12.                super.onCreate(savedInstanceState);

  13.                setContentView(R.layout.main);


  14.                //找到拖动条和文本框

  15.                final SeekBar sb = (SeekBar) findViewById(R.id.SeekBar01);

  16.                final TextView tv1 = (TextView) findViewById(R.id.TextView01);


  17.                //设置拖动条的初始值和文本框的初始值

  18.                sb.setMax(100);

  19.                sb.setProgress(30);

  20.                tv1.setText("当前进度:" + sb.getProgress());


  21.                //设置拖动条改变监听器

  22.                OnSeekBarChangeListener osbcl = new OnSeekBarChangeListener() {


  23.                        @Override

  24.       &n