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

//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)+"%");


}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值