您的位置:首页 > 其它

ProgressBar实现进度条及ProgressDialog实现对话框进度条

2015-06-12 11:06 357 查看
//xxx程序猿

package com.example.jindu;

import android.support.v7.app.ActionBarActivity;

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 implements OnClickListener{

private ProgressBar pb;

private Button bt_add;

private Button bt_reduce;

private Button bt_reset;

private Button bt_dh;

private TextView textview;

private ProgressDialog proDialog;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//启用窗口特征。启用带进度和不带进度进度条

requestWindowFeature(Window.FEATURE_PROGRESS);

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

setContentView(R.layout.main);

//显示两种进度条

setProgressBarVisibility(true);

setProgressBarIndeterminateVisibility(true);

//最大刻度常量是10000,现在所占是600/10000

setProgress(600);

init();

}

private void init() {

// TODO Auto-generated method stub

pb=(ProgressBar)findViewById(R.id.PB);

bt_add=(Button)findViewById(R.id.bt_add);

bt_add.setOnClickListener(this);

bt_reduce=(Button)findViewById(R.id.bt_reduce);

bt_reduce.setOnClickListener(this);

bt_reset=(Button)findViewById(R.id.bt_chong);

bt_reset.setOnClickListener(this);

bt_dh=(Button) findViewById(R.id.bt_duihuakuan);

bt_dh.setOnClickListener(this);

textview=(TextView) findViewById(R.id.TV);

//获得第一刻度

int first=pb.getProgress();

//获得第二刻度

int second=pb.getSecondaryProgress();

//获得最大刻度

int max=pb.getMax();

textview.setText("第一进度条进度百分百:"+(int)((first/(float)max)*100)+"%"+"第二进度条进度百分百:"+(int)((second/(float)max)*100)+"%");

}

@Override

public void onClick(View view) {

// TODO Auto-generated method stub

switch(view.getId())

{

case R.id.bt_add:

{

//增加刻度

pb.incrementProgressBy(10);

pb.incrementSecondaryProgressBy(10);

break;

}

case R.id.bt_reduce:

{

//减少刻度

pb.incrementProgressBy(-10);

pb.incrementSecondaryProgressBy(-10);

break;

}

case R.id.bt_chong:

{

//重置刻度

pb.setProgress(30);

pb.setSecondaryProgress(60);

break;

}

case R.id.bt_duihuakuan:

{

//创建ProgressDialog对象

proDialog=new ProgressDialog(this);

//设置显示风格,横向进度条

proDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

//设置标题

proDialog.setTitle("xxx程序猿");

//设置对话框耳朵文字信息

proDialog.setMessage("欢迎来到xxx程序猿课堂");

//设置图标

proDialog.setIcon(R.drawable.ic_launcher);

//设置最大进度

proDialog.setMax(100);

//设置初始化已经增长到的进度

proDialog.incrementProgressBy(50);

//进度条明确显示进度

proDialog.setIndeterminate(false);

//在ProgressDialog设置一个按钮

proDialog.setButton("确定" ,new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface arg0, int arg1) {

// TODO Auto-generated method stub

Toast.makeText(MainActivity.this, "谢谢大家的支持", 0).show();

}

});

//设置退出对话框

proDialog.setCancelable(true);

//最后显示proDialog

proDialog.show();

}

break;

}

textview.setText("第一进度条进度百分百:"+(int)((pb.getProgress()/(float)pb.getMax())*100)+"%"+"第二进度条进度百分百:"+(int)((pb.getSecondaryProgress()/(float)pb.getMax())*100)+"%");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: