Android_ProgressBar实现进度条

XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
       tools:context="com.example.jindutiao.MainActivity" >

    <ProgressBar
        android:id="@+id/progressBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/progressBar1"
        android:layout_below="@+id/progressBar1"
        android:layout_marginTop="30dp" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="49dp"
        android:layout_marginTop="26dp" />

    <ProgressBar
        android:id="@+id/progressBar3"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:progress="50"
        android:secondaryProgress="80"
        android:max="100" 
        />

    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/progressBar3"
        android:layout_below="@+id/progressBar3"
        android:layout_marginTop="21dp"
        android:text="@string/_add" />

    <Button
        android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/reduce"
        android:layout_alignBottom="@+id/reduce"
        android:layout_alignParentRight="true"
        android:text="重置" />

    <Button
        android:id="@+id/reduce"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/add"
        android:layout_alignBottom="@+id/add"
        android:layout_toRightOf="@+id/progressBar2"
        android:text="减少" />
    <TextView 
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

    <Button
        android:id="@+id/show"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/add"
        android:layout_marginTop="38dp"
        android:layout_toLeftOf="@+id/reduce"
        android:text="@string/_show" />


</RelativeLayout>

MainActivuty.java文件:

package com.example.jindutiao;

import android.app.Activity;
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 Activity{
    private ProgressBar progress;
    private Button add;
    private Button reduce;
    private Button reset;
    private TextView textView;
    private ProgressDialog pd;
    private Button show;

    @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(false);
        setProgress(600);

        init();
    }

    public void init() {
        // TODO Auto-generated method stub
        progress = (ProgressBar) findViewById(R.id.progressBar3);
        add = (Button) findViewById(R.id.add);
        reduce = (Button) findViewById(R.id.reduce);
        reset = (Button) findViewById(R.id.reset);
        textView = (TextView) findViewById(R.id.textView);
        show = (Button) findViewById(R.id.show);
        /*//获取第一条进度条的进度
        int first  = progress.getProgress();
        //获取第二条进度条的进度
        int second = progress.getSecondaryProgress();
        //获取进度条的最大进度
        int max = progress.getMax();
        textView.setText("第一进度条:"+(int)(first)/(float)(max)*100
                +"%  第二进度条:"+(int)(second)/(float)(max)*100+"%");*/
        textView.setText("第一进度条:"+(int)(progress.getProgress())/(float)(progress.getMax())*100+
        "%  第二进度条:"+(int)(progress.getSecondaryProgress())/(float)(progress.getMax())*100+"%");

        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //第一进度和第二进度都增加10
                progress.incrementProgressBy(10);
                progress.incrementSecondaryProgressBy(10);
                textView.setText("第一进度条:"+(int)(progress.getProgress())/(float)(progress.getMax())*100+
                        "%  第二进度条:"+(int)(progress.getSecondaryProgress())/(float)(progress.getMax())*100+"%");
            }
        });
        reduce.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                //第一进度和第二进度都减少10

                progress.incrementProgressBy(-10);
                progress.incrementSecondaryProgressBy(-10);
                textView.setText("第一进度条:"+(int)(progress.getProgress())/(float)(progress.getMax())*100+
                        "%  第二进度条:"+(int)(progress.getSecondaryProgress())/(float)(progress.getMax())*100+"%");
            }
        });
        reset.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //重置
                progress.setProgress(0);
                progress.setSecondaryProgress(0);
                textView.setText("第一进度条:"+(int)(progress.getProgress())/(float)(progress.getMax())*100+
                        "%  第二进度条:"+(int)(progress.getSecondaryProgress())/(float)(progress.getMax())*100+"%");

            }
        });
        show.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                /*
                 * 页面显示风格
                 * */
                //新建 ProgressDialog对象
                pd = new ProgressDialog(MainActivity.this);
                //设置显示风格
                pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                //设置对话框里的文字信息
                pd.setTitle("对话框进度条");
                //设置图标
                pd.setIcon(R.drawable.ic_launcher);

                /*
                 * 设定关于 ProgressBar的一些属性
                 * */
                //设定最大进度
                pd.setMax(100);
                //设定初始化已经增加到的长度
                pd.incrementProgressBy(30);
                //进度条明确显示进度的
                pd.setIndeterminate(false);
                /*
                 * 设定一个确定按钮
                 * */
                pd.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Toast.makeText(MainActivity.this, "欢迎访问", Toast.LENGTH_SHORT).show();

                    }
                });

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

                //现实 ProgressDialog
                pd.show();

            }
        });

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值