API Demos 2.3 学习笔记 (14)-- Views->Progress Bar

简介:

更多精彩内容,请点击阅读:《API Demos 2.3 学习笔记》


进度条ProgressBar可以用来显示某项操作(比如下载文件)的当前进度。ProgressBar主要有两种模式:可以随时确定当前进度值的模式(progressBarStyleHorizontal风格),无法确定当前进度值的模式(其他7种风格)。另外,ProgressBar还有8种系统内置的风格:

  • progressBarStyle 默认风格
  • progressBarStyleHorizontal 水平进度条风格
  • progressBarStyleInverse  反色中等大小风格
  • progressBarStyleLarge  超大风格
  • progressBarStyleLargeInverse 反色超大风格
  • progressBarStyleSmall  超小风格
  • progressBarStyleSmallInverse 反色超小风格
  • progressBarStyleSmallTitle  标题小风格
这些风格的ProgressBar预览效果如下所示:
   

以上几种风格的设置方法是在xml布局文件中ProgressBar控件内设置style(默认风格progressBarStyle可以不标明):
        style="?android:attr/progressBarStyle"
        style="?android:attr/progressBarStyleHorizontal"
        style="?android:attr/progressBarStyleInverse"
        style="?android:attr/progressBarStyleLarge"
        style="?android:attr/progressBarStyleLargeInverse"
        style="?android:attr/progressBarStyleSmall"
        style="?android:attr/progressBarStyleSmallInverse"
        style="?android:attr/progressBarStyleSmallTitle"

ProgressBar不仅可以放在主界面中,而且还能放在标题栏中,甚至还可以放在对话框中。这里我们为了便于理解,按照ProgressBar的模式和用途分成六种情况进行介绍。
1、主界面中的水平进度条
	<ProgressBar android:id="@+android:id/progressBarStyleHorizontal"
		style="?android:attr/progressBarStyleHorizontal"
		android:layout_width="120dip"
		android:layout_height="wrap_content"
		android:max="100"
		android:progress="50"
		android:secondaryProgress="75" />
注:android:max="100" 属性表示该ProgressBar 的最大值为100,即取值范围为0~100
android:progress="50" 属性表示当前初始的进度值为50
android:secondaryProgress="75"  属性表示当前初始的第二进度值为 75

2、主界面中的环形进度条
	<ProgressBar android:id="@+android:id/progressBarStyle"
		style="?android:attr/progressBarStyle"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" />
注:style可以取值除了 progressBarStyleHorizontal之外的另外7个风格取值。

3、标题栏中的水平进度条
        //请求窗口特色风格,这里设置成明确的进度风格  
      requestWindowFeature(Window.FEATURE_PROGRESS);
      setContentView(R.layout.main);

       //设置标题栏上ProgressBar的主要进度值和第二进度值
       //标题栏上ProgressBar的取值范围为0~10000
      setProgress(5000);
      setSecondaryProgress(7500);                   
      
      //显示标题上的进度条(确定进度值)
      setProgressBarVisibility(true);
注:1、requestWindowFeature(Window.FEATURE_PROGRESS);一句必须放在 setContentView(R.layout.main);之前,否则程序报错。
2、setProgressBarVisibility(true);用来设置进度条是否可见。setProgress(5000);和setSecondaryProgress(7500);  分别用来设置进度条的主要进度值和第二进度值。 这几句必须放在 setContentView(R.layout.main);之后,否则程序报错。

4、标题栏中的环形进度条
         //请求窗口特色风格,这里设置成不明确的进度风格
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.main);
     
        //显示标题上的进度条(不确定进度值)
        setProgressBarIndeterminateVisibility(true);
注:1、 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 一句必须放在 setContentView(R.layout.main);之前,否则程序报错。
2、 setProgressBarIndeterminateVisibility(true);用来设置进度条是否可见。这一句必须放在 setContentView(R.layout.main);之后,否则程序报错。

5、进度对话框中的水平进度条
        //创建ProgressDialog
        final ProgressDialog mProgressDialog = new ProgressDialog(this);  
        
         //设置ProgressDialog对象类型为环形
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        
        //设置ProgressDialog的图标
        mProgressDialog.setIcon(R.drawable.icon);
        
        //设置ProgressDialog的标题
        mProgressDialog.setTitle("ProgressDialog");
        
        //设置ProgressDialog的消息
        mProgressDialog.setMessage("Loading...");
        
        //设置ProgressDialog中进度条最大值100,即取值范围为0~100
        mProgressDialog.setMax(100);
        
        //设置ProgressDialog中进度条当前初始主要进度值为 50
        mProgressDialog.setProgress(0);
       
        //设置ProgressDialog中进度条当前初始第二进度值为 75
        mProgressDialog.setSecondaryProgress(0);       
       
        //设置ProgressDialog的当前进度值是否不确定
        mProgressDialog.setIndeterminate(false);
        
        //设置ProgressDialog是否可以通过回退键取消
        mProgressDialog.setCancelable(true);
        
        //设置ProgressDialog的确定按钮以及监听器
        mProgressDialog.setButton( DialogInterface.BUTTON_POSITIVE,"Ok", new  DialogInterface.OnClickListener(){

			public void onClick(DialogInterface dialog, int which) {
				// TODO 执行Button_OK按钮点击后的响应动作
				mProgressDialog.dismiss();				
			}
        	
        });
        
        //设置ProgressDialog的取消按钮以及监听器
       mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"Cancel", new  DialogInterface.OnClickListener(){

			public void onClick(DialogInterface dialog, int which) {
				// TODO 执行Button_CANCEL按钮点击后的响应动作
				mProgressDialog.dismiss();
			}
        	
        });
       
       //显示ProgressDialog
        mProgressDialog.show();
注:直接调用setProgress和setSecondaryProgress无法真正改变进度条的主进度值和第二进度值。关于如何修改进度值,请点击阅读: 《android dialog ——ProgressDialog 进度条对话框详解》

6、进度对话框中的环形进度条
       //创建ProgressDialog
        final ProgressDialog mProgressDialog = new ProgressDialog(this);  
        
         //设置ProgressDialog对象类型为环形
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        
        //设置ProgressDialog的图标
        mProgressDialog.setIcon(R.drawable.icon);
        
        //设置ProgressDialog的标题
        mProgressDialog.setTitle("ProgressDialog");
        
        //设置ProgressDialog的消息
        mProgressDialog.setMessage("Loading...");
        
        //设置ProgressDialog的当前进度值是否不确定
        mProgressDialog.setIndeterminate(true);
        
        //设置ProgressDialog是否可以通过回退键取消
        mProgressDialog.setCancelable(true);
        
        //设置ProgressDialog的确定按钮以及监听器
        mProgressDialog.setButton( DialogInterface.BUTTON_POSITIVE,"Ok", new  DialogInterface.OnClickListener(){

			public void onClick(DialogInterface dialog, int which) {
				// TODO 执行Button_OK按钮点击后的响应动作
				mProgressDialog.dismiss();				
			}
        	
        });
        
        //设置ProgressDialog的取消按钮以及监听器
       mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"Cancel", new  DialogInterface.OnClickListener(){

			public void onClick(DialogInterface dialog, int which) {
				// TODO 执行Button_CANCEL按钮点击后的响应动作
				mProgressDialog.dismiss();
			}
        	
        });
       
       //显示ProgressDialog
    mProgressDialog.show();


下面我们进行实例代码解析:
1、Incremental
res-value-string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="progressbar_1_plus">+</string>
    <string name="progressbar_1_minus">-</string>
    <string name="progressbar_1_default_progress">Default progress:</string>
    <string name="progressbar_1_secondary_progress">Secondary progress:</string>
</resources>

res-layout-progressbar_1.xml
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

	<!-- 一个ProgressBar对象,类型为:progressBarStyleHorizontal -->
    <ProgressBar android:id="@+id/progress_horizontal"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="75" />

	<!-- 一个TextView对象,提示下面两个按钮改变的主要进度值-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/progressbar_1_default_progress" />        

 	<!-- 水平布局内放置两个按钮,分别用于增大和减小进度条的主要进度值 -->
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button android:id="@+id/decrease"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/progressbar_1_minus" />

        <Button android:id="@+id/increase"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/progressbar_1_plus" />

    </LinearLayout>

 	<!-- 一个TextView对象,提示下面两个按钮改变的第二进度值-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/progressbar_1_secondary_progress" />        

  	<!-- 水平布局内放置两个按钮,分别用于增大和减小进度条的第二进度值 -->
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button android:id="@+id/decrease_secondary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/progressbar_1_minus" />

        <Button android:id="@+id/increase_secondary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/progressbar_1_plus" />

    </LinearLayout>

</LinearLayout>

src-com.example.android.apis.view-ProgressBar1.java
package com.example.android.apis.view;

import com.example.android.apis.R;

import android.app.Activity;
import android.widget.Button;
import android.widget.ProgressBar;
import android.os.Bundle;
import android.view.View;
import android.view.Window;


/**
 * 演示如何在窗口标题栏中使用进度条。
 * 进度条将会一直显示,直到进度完成,此时进度条消失。
 */
public class ProgressBar1 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 请求在标题栏中显示进度条
        requestWindowFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.progressbar_1);
        
        //设置标题栏中的进度条可见
        setProgressBarVisibility(true);
        
        // 通过findViewById方法获得一个ProgressBar对象
        final ProgressBar progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);
        
         //获取ProgressBar对象(取值范围0~100)的主要进度值和第二进度值,经过转换后设置到标题栏中的ProgressBar对象(取值范围0~10000)。
        setProgress(progressHorizontal.getProgress() * 100);
        setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);
        
        //该按钮用于增大ProgressBar的主要进度值,步进值为1
        Button button = (Button) findViewById(R.id.increase);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
            	 //progressHorizontal主要进度值加1
                progressHorizontal.incrementProgressBy(1);
                
                //标题栏中的进度条取值范围0~10000,而progressHorizontal取值范围0~100,因此,获取progressHorizontal进度值,乘以100,设置成标题栏上进度条的进度值。下同。
                setProgress(100 * progressHorizontal.getProgress());
            }
        });

        //该按钮用于减小ProgressBar的主要进度值,步进值为1
        button = (Button) findViewById(R.id.decrease);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
           	 //progressHorizontal主要进度值减1
               progressHorizontal.incrementProgressBy(-1);

               setProgress(100 * progressHorizontal.getProgress());
            }
        });

        //该按钮用于增大ProgressBar的第二进度值,步进值为1
        button = (Button) findViewById(R.id.increase_secondary);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                //progressHorizontal第二进度值加1
                progressHorizontal.incrementSecondaryProgressBy(1);
                
                  //标题栏中的进度条取值范围0~10000,而progressHorizontal取值范围0~100,因此,获取progressHorizontal第二进度值,乘以100,设置成标题栏上进度条的第二进度值。下同。
                setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
            }
        });

        //该按钮用于减小ProgressBar的第二进度值,步进值为1
        button = (Button) findViewById(R.id.decrease_secondary);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                //progressHorizontal第二进度值减1
                progressHorizontal.incrementSecondaryProgressBy(-1);

                setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
            }
        });
        
    }
}


2、Smooth
res-layout-progressbar_2.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

	<!-- 一个ProgressBar对象,类型为:progressBarStyleLarge -->
    <ProgressBar android:id="@+android:id/progress_large"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

	<!-- 一个ProgressBar对象,类型为默认类型:progressBarStyle -->
    <ProgressBar android:id="@+android:id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

 	<!-- 一个ProgressBar对象,类型为默认类型:progressBarStyleSmall -->
    <ProgressBar android:id="@+android:id/progress_small"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

  	<!-- 一个ProgressBar对象,类型为默认类型:progress_small_title -->
    <ProgressBar android:id="@+android:id/progress_small_title"
        style="?android:attr/progressBarStyleSmallTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

src-com.example.android.apis.view-ProgressBar2.java
package com.example.android.apis.view;

import com.example.android.apis.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;


/**
 * 演示如何在窗口标题栏中使用不确定进度的进度条。
 * 本实例演示了3中不同大小的环形进度条,
 */
public class ProgressBar2 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 请求在标题栏中显示不确定进度类型的进度条
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        
        setContentView(R.layout.progressbar_2);

        // 设置标题栏中的进度条可见
        setProgressBarIndeterminateVisibility(true);
    }
}



3、Dialogs
res-value-string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="progressbar_3_progress">Show Progress</string>
    <string name="progressbar_3_indeterminate">Show Indeterminate</string>
    <string name="progressbar_3_indeterminate_no_title">Show Indeterminate No Title</string>
</resources>

res-layout-progressbar_3.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <!-- 一个Button对象,点击显示一个带有标题的ProgressDialog -->
    <Button android:id="@+id/showIndeterminate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/progressbar_3_indeterminate" />

     <!-- 一个Button对象,点击显示一个不带标题的ProgressDialog -->
    <Button android:id="@+id/showIndeterminateNoTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/progressbar_3_indeterminate_no_title" />

</LinearLayout>


src-com.example.android.apis.view-ProgressBar3.java

package com.example.android.apis.view;

import com.example.android.apis.R;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * 演示如何使用进度对话框
 */
public class ProgressBar3 extends Activity {

    ProgressDialog mDialog1;
    ProgressDialog mDialog2;

    private static final int DIALOG1_KEY = 0;
    private static final int DIALOG2_KEY = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.progressbar_3);

        Button button = (Button) findViewById(R.id.showIndeterminate);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG1_KEY); //显示带有标题的进度对话框
            }
        });

        button = (Button) findViewById(R.id.showIndeterminateNoTitle);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG2_KEY); //显示不带标题的进度对话框
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG1_KEY: {           	  
                ProgressDialog dialog = new ProgressDialog(this);  //创建ProgressDialog
                dialog.setTitle("Indeterminate"); //设置标题
                dialog.setMessage("Please wait while loading..."); //设置主题信息
                dialog.setIndeterminate(true);  //设置进度条为不明确进度的类型(环形)
                dialog.setCancelable(true);  //设置窗口可以通过退回键取消
                return dialog;
            }
            case DIALOG2_KEY: {
                ProgressDialog dialog = new ProgressDialog(this); //创建ProgressDialog
                dialog.setMessage("Please wait while loading...");  //设置主题信息
                dialog.setIndeterminate(true);  //设置进度条为不明确进度的类型(环形)
                dialog.setCancelable(true); //设置窗口可以通过退回键取消
                return dialog;
            }
        }
        return null;
    }
}


4、In Title Bar
res-value-string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="progressbar_4_toggle">Toggle Indeterminate</string>
</resources>

res-layout-progressbar_4.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content">

    <!-- 一个Button对象,点击显示/隐藏标题栏上的进度条 -->
    <Button android:id="@+id/toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/progressbar_4_toggle" />

</LinearLayout>

src-com.example.android.apis.view-ProgressBar4.java

package com.example.android.apis.view;

import com.example.android.apis.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.View;
import android.widget.Button;


/**
 * 演示如何在标题栏中使用不确定进度值的进度条
 */
public class ProgressBar4 extends Activity {
    private boolean mToggleIndeterminate = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 请求在标题栏中显示不确定进度类型的进度条
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.progressbar_4);
        
        // 设置标题栏中的进度条是否可见
        setProgressBarIndeterminateVisibility(mToggleIndeterminate);
        
        //点击一次,改变一次mToggleIndeterminate值
        Button button = (Button) findViewById(R.id.toggle);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                mToggleIndeterminate = !mToggleIndeterminate;
                setProgressBarIndeterminateVisibility(mToggleIndeterminate);
            }
        });
    }
}

知识点1:关于Progress的自定义等知识,请点击阅读: 《Android自定义ProgressDialog的方法》《自定义ProgressBar》以及 《多式样ProgressBar》


预览效果;

       

       

相关文章
|
8月前
|
前端开发 API
前端学习笔记202305学习笔记第三十天-什么是mvc-c层api 前后端联动1
前端学习笔记202305学习笔记第三十天-什么是mvc-c层api 前后端联动1
51 0
|
8月前
|
前端开发 API
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 前后端联动3
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 前后端联动3
42 0
|
8月前
|
前端开发 API
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 和mvc总结3
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 和mvc总结3
32 0
|
8月前
|
前端开发 API
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 和mvc总结4
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 和mvc总结
43 0
|
8月前
|
前端开发 API
前端学习笔记202305学习笔记第三十天-什么是mvc-c层api 前后端联动3
前端学习笔记202305学习笔记第三十天-什么是mvc-c层api 前后端联动3
47 0
|
8月前
|
前端开发 API
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 和mvc总结2
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 和mvc总结2
40 0
|
8月前
|
前端开发 API
前端学习笔记202305学习笔记第二十二天-新增修改api的封装
前端学习笔记202305学习笔记第二十二天-新增修改api的封装2
35 0
|
17天前
|
缓存 前端开发 API
API接口封装系列
API(Application Programming Interface)接口封装是将系统内部的功能封装成可复用的程序接口并向外部提供,以便其他系统调用和使用这些功能,通过这种方式实现系统之间的通信和协作。下面将介绍API接口封装的一些关键步骤和注意事项。
|
24天前
|
监控 前端开发 JavaScript
实战篇:商品API接口在跨平台销售中的有效运用与案例解析
随着电子商务的蓬勃发展,企业为了扩大市场覆盖面,经常需要在多个在线平台上展示和销售产品。然而,手工管理多个平台的库存、价格、商品描述等信息既耗时又容易出错。商品API接口在这一背景下显得尤为重要,它能够帮助企业在不同的销售平台之间实现商品信息的高效同步和管理。本文将通过具体的淘宝API接口使用案例,展示如何在跨平台销售中有效利用商品API接口,以及如何通过代码实现数据的统一管理。
|
1月前
|
安全 算法 API
产品经理必备知识——API接口
前言 在古代,我们的传输信息的方式有很多,比如写信、飞鸽传书,以及在战争中使用的烽烟,才有了著名的烽火戏诸侯,但这些方式传输信息的效率终究还是无法满足高速发展的社会需要。如今万物互联的时代,我通过一部手机就可以实现衣食住行的方方面面,比如:在家购物、远程控制家电、自动驾驶等等,背后都离不开我们今天要聊的API接口。