Android学习--ProgressBar进度条的使用

MainActivity.java

package com.example.demo5;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
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 implements OnClickListener {

    private Button add;
    private Button reduce;
    private Button reset;
    private TextView text;
    private ProgressBar progressBar;
    private Button show;
    private ProgressDialog proDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 启用窗口特征,启动不带进度的进度条
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        // 启用窗口特征,启动带进度的进度条
        requestWindowFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.main);
        // 显示带进度条进度条
        setProgressBarVisibility(true); 
        // 显示不带进度条进度条
        setProgressBarIndeterminateVisibility(true);
        // Max=10000
        setProgress(1000);
        // 初始化控件方法
        init();
        // 加载按钮
        add.setOnClickListener(this);
        reduce.setOnClickListener(this);
        reset.setOnClickListener(this);
        show.setOnClickListener(this);
    }

    // 初始化控件实现
    private void init() {
        add = (Button) findViewById(R.id.add);
        reduce = (Button) findViewById(R.id.reduce);
        reset = (Button) findViewById(R.id.reset);
        text = (TextView) findViewById(R.id.text);
        progressBar = (ProgressBar) findViewById(R.id.progressBar4);
        show = (Button) findViewById(R.id.show);
        // 获取第一个进度条
        int first = progressBar.getProgress();
        // 获取第二个进度条
        int second = progressBar.getSecondaryProgress();
        // 获取最大进度条
        int max = progressBar.getMax();
        // 设置文本信息
        text.setText("第一个进度条百分比:" + (int) (first / (float) max * 100) + "%"
                + "  " + "第二个进度条百分比:" + (int) (second / (float) max * 100) + "%");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.add: {
            // 设置第一进度条增加5刻度
            progressBar.incrementProgressBy(5);
            // 设置第二进度条增加5刻度
            progressBar.incrementSecondaryProgressBy(5);
            break;
        }
        case R.id.reduce: {
            // 设置第一进度条减少5刻度
            progressBar.incrementProgressBy(-5);
            // 设置第二进度条减少5刻度
            progressBar.incrementSecondaryProgressBy(-5);
            break;
        }
        case R.id.reset: {
            // 设置第一进度条
            progressBar.setProgress(50);
            // 设置第二进度条
            progressBar.setSecondaryProgress(80);
            break;
        }
        case R.id.show: {
            /**
             * 设置prograssDialog的界面
             */
            // 初始化prograssDialog
            proDialog = new ProgressDialog(MainActivity.this);
            // 设置显示风格--水平(显示刻度)
            proDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            // 设置图标
            proDialog.setIcon(R.drawable.fight);
            // 设置标题
            proDialog.setTitle("设置进度条");
            // 设置对话框中的文字信息
            proDialog.setMessage("Fight big man~");

            /**
             * 设置prograssBar的一些属性
             */
            // 设置最大进度
            proDialog.setMax(100);
            // 设定初始化长度
            proDialog.incrementProgressBy(50);
            // 设置进度条是否显示进度--是
            proDialog.setIndeterminate(false);

            /**
             * 设置确定按钮
             */
            proDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // 弹出提示信息
                    Toast.makeText(MainActivity.this, "显示成功", Toast.LENGTH_SHORT).show();
                }
            });

            // 是否通过返回按钮退出对话框
            proDialog.setCancelable(true);

            // 显示prograssDialog
            proDialog.show();
            break;
        }
        }
        // 设置文本信息
        text.setText("第一个进度条百分比:" + (int) (progressBar.getProgress()
                        / (float) progressBar.getMax() * 100) + "%" + "  "
                + "第二个进度条百分比:" + (int) (progressBar.getSecondaryProgress()
                        / (float) progressBar.getMax() * 100) + "%");
    }

}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_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:style/Widget.ProgressBar.Horizontal"
        android:progressDrawable="@drawable/progress_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="80" />

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

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

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

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

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

</LinearLayout>

progress_bar.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="5dip" />

        <solid android:color="#88000000"/>
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />

                <gradient
                    android:angle="270"
                    android:centerColor="#C6B7FE"
                    android:centerY="0.75"
                    android:endColor="#C3B2FF"
                    android:startColor="#B9A4FF" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />

            <gradient
                android:angle="270"
                android:centerColor="#64EBFF"
                android:centerY="0.75"
                android:endColor="#8EEFFF"
                android:startColor="#57E8FF" />
            </shape>
        </clip>
    </item>

</layer-list>

注:理解下句

// 是否通过返回按钮退出对话框
proDialog.setCancelable(true);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值