Android进度条控件ProgressBar使用

ProgressBar有四种样式,圆形的(大,中,小)和直条形的(水平)

对应的style为

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

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar3"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar4"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

注:style不写默认为中型进度条

使用requestWindowFeature来实现带进度条的标题栏

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //启用精确显示进度的进度条和不显示精度的进度条标题栏
        requestWindowFeature(Window.FEATURE_PROGRESS);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);

        //设置可见性
        setProgressBarVisibility(true);
        setProgressBarIndeterminateVisibility(true);
        //设置精确进度条的值,默认10000上限,达到10000后进度条不显示
        setProgress(9999);
    }

}

在activity中对ProgressBar进行简单操作

用TextView实时显示第一进度条和第二进度条

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity implements OnClickListener{
    private ProgressBar progress;
    private Button add, reduce, reset;
    private TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置无标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        //初始化
        init();
    }

    private void init() {
        progress = (ProgressBar) findViewById(R.id.horiz);
        add = (Button) findViewById(R.id.add);
        reduce  = (Button) findViewById(R.id.reduce);
        reset  = (Button) findViewById(R.id.reset);
        text =  (TextView) findViewById(R.id.textView1);

        //获取第一进度条进度
        int first = progress.getProgress();
        //获取第二进度条进度
        int second = progress.getSecondaryProgress();
        //获取最大进度
        int max = progress.getMax();
        text.setText(
                "第一进度百分比:"+(int)(first/(float)max*100)+
                "% 第二进度百分比:"+(int)(second/(float)max*100)+"%");
        add.setOnClickListener(this);
        reduce.setOnClickListener(this);
        reset.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.add:
            //增加第一进度条和第二进度条
            progress.incrementProgressBy(10);
            progress.incrementSecondaryProgressBy(10);
            break;
        case R.id.reduce:
            progress.incrementProgressBy(-10);
            progress.incrementSecondaryProgressBy(-10);
            break;
        case R.id.reset:
            progress.setProgress(50);
            progress.setSecondaryProgress(80);
            break;
        }
        text.setText(
                "第一进度百分比:"+(int)(progress.getProgress()/(float)progress.getMax()*100)+
                "% 第二进度百分比:"+(int)(progress.getSecondaryProgress()/(float)progress.getMax()*100)+"%");

    }

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

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar3"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/horiz"
        android:max="100"
        android:secondaryProgress="80"
        android:progress="50"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

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

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

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

使用Dialog形式

import android.support.v7.app.ActionBarActivity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity{
    private ProgressDialog progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置无标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        init();
    }

    private void init() {
        //设置ProgressDialog的显示

                //新建对象
                progress = new ProgressDialog(this);
                //设置风格
                progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                //设置标题
                progress.setTitle("reading");
                //设置文本内容
                progress.setMessage("please waiting");
                //设置图标
                progress.setIcon(R.drawable.ic_launcher);

                progress.setMax(100);
                progress.incrementProgressBy(50);
                //是否非精确显示
                progress.setIndeterminate(false);
                progress.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //此处不能写this
                        Toast.makeText(MainActivity.this, "ok...", Toast.LENGTH_SHORT).show();
                    }
                });
                //是否可以点击取消键(android自带的返回键)
                progress.setCancelable(false);
                //显示
                progress.show();
    }

}

时间: 2024-08-29 20:33:40

Android进度条控件ProgressBar使用的相关文章

[转载]ExtJs4 笔记(8) Ext.slider 滚轴控件、 Ext.ProgressBar 进度条控件、 Ext.Editor 编辑控件

作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律责任. 本篇要登场的有三个控件,分别是滚轴控件.进度条控件和编辑控件. 一.滚轴控件 Ext.slider 1.滚轴控件的定义 下面我们定义三个具有代表意义滚轴控件,分别展示滚轴横向.纵向,以及单值.多值选择的特性: [html] <h1>滚轴控件</h1> <div class

Photoshop和WPF双剑配合,打造炫酷个性的进度条控件

现在如果想打造一款专业的App,UI的设计和操作的简便性相当重要.UI设计可以借助Photoshop或者AI等设计工具,之前了解到WPF设计工具Expression Blend可以直接导入PSD文件或者AI设计文件(当然不是全部特征支持),最近研究了一下,也废了一番周折,好在最后实现了预期的效果.下面将step by step用示例说明如何先用PS构建一个矢量图形模板,然后用Expression Blend导入PSD文件,并获取PATH的Data值,为打造一款炫酷的个性进度条控件构建美观UI.

进度条控件基本使用

进度条控件基本使用 一.简介 二.方法 三.代码实例 四.注意点 五.易错点

用 CALayer 定制下载进度条控件

// // RPProgressView.h // CALayer定制下载进度条控件 // // Created by RinpeChen on 16/1/2. // Copyright © 2016年 rinpe. All rights reserved. // #import <UIKit/UIKit.h> @interface RPProgressView : UIView @property (nonatomic, assign) CGFloat progressValue; // 进

iOS:进度条控件的详细使用

进度条控件:UIProcessView:UIView 功能:顾名思义,用来显示下载进度或者传输数据进度. 属性: @property(nonatomic) UIProgressViewStyle progressViewStyle; //风格类型 @property(nonatomic) float progress;                                   //当前进度 @property(nonatomic, retain) UIColor* progressTi

为OLED屏增加GUI支持6:进度条控件

本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN10 开发环境:MDK5.13 MCU:STM32F103 源代码: gui_widget_progbar.h /** * Copyright (c), 2015-2025 * @file gui_widget_progbar.h * @brief 文本控件头文件 * @author jdh * @date 2015/11/22 */ #ifndef _GUI_WIDGET_PRO

为OLED屏添加GUI支持6:进度条控件

本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN10 开发环境:MDK5.13 MCU:STM32F103 源码: gui_widget_progbar.h /** * Copyright (c), 2015-2025 * @file gui_widget_progbar.h * @brief 文本控件头文件 * @author jdh * @date 2015/11/22 */ #ifndef _GUI_WIDGET_PROG

MFC进度条控件(Progress)

进度条控件的应用 进度条的主要方法和事件 SetRange方法:该方法用于设置进度条范围. void SetRange(short  nLower,short  nUpper);进度的下界和上界范围. GetPos方法:用于获取进度条当前位置. SetStep方法:用于设置进度条每步的增量. StepIt方法:用于每一步的增量来增加进度条的当前位置. 详细的介绍见MFC++程序开发参考大全(P196) 1.创建一个基于对话框的应用程序. 2.向对话框中添加一个进度条控件.一个按钮控件.3个静态文

进度条控件CProgressCtrl实现进度滚动效果

关于CProgressCtrl 控件的基本操作网上有很多资料,可我想实现进度条中进度滚动效果,即很多时候程序出现的等待或启动画面,如下图: 实现这个效果的函数为SetMarquee(_In_ BOOL fMarqueeMode, _In_ int nInterval),第一个参数为开关,第二个参数指定滚动时间.且控件应该包含 PBS_MARQUEE风格,eg:m_ProgressCtl.Create(WS_CHILD|WS_VISIBLE|PBS_MARQUEE,CRect(100,200,25