progressBar和ProgressDialog使用

本文详细介绍了在Android开发中如何使用进度条和ProgressDialog。包括XML配置中的样式设置、代码中进度值的调整方法,以及ProgressDialog的不同创建和展示方式。此外,还提供了使用ProgressDialog时的一些高级设置技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.在xml中设置的时候注意这几个值就可以了

style="@android:style/Widget.ProgressBar.Horizontal" // style可以设定它显示的方式,这里的水平的显示,其他的可以自己试一下.
android:indeterminate="true"//设置了当前为无模式进度条

二.在代码中有两个设定要注意

progressBar.setProgress();//设置进度值这里写代码片
progressBar.setSecondaryProgress();//设置第二进度值

三.这里在介绍一下progressDialog
有几种创建方式:

// 方式一:new Dialog  
ProgressDialog dialog = new ProgressDialog(this);  
dialog.show();  
// 方式二:使用静态方式创建并显示,这种进度条只能是圆形条,设置title和Message提示内容  
ProgressDialog dialog2 = ProgressDialog.show(this, "提示", "正在登陆中");  
// 方式三 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数boolean indeterminate设置是否是不明确的状态  
ProgressDialog dialog3 = ProgressDialog.show(this, "提示", "正在登陆中", false);  
// 方式四 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数boolean cancelable 设置是否进度条是可以取消的  
ProgressDialog dialog4 = ProgressDialog.show(this, "提示", "正在登陆中", false, true);  
// 方式五 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数 DialogInterface.OnCancelListener  
// cancelListener用于监听进度条被取消  
ProgressDialog dialog5 = ProgressDialog.show(this, "提示", "正在登陆中", true, true, cancelListener);  
private OnCancelListener cancelListener = new OnCancelListener() {  

    @Override  
    public void onCancel(DialogInterface dialog) {  
        // TODO Auto-generated method stub  
        Toast.makeText(MainActivity.this, "进度条被取消", Toast.LENGTH_LONG)  
                .show();  

    }  
}; 

下面是完整的使用,如果你想更完善的使用它的话.(请认真看注释)

ProgressDialog dialog = new ProgressDialog(this);  
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                                            // 设置进度条的形式为圆形转动的进度条
                                            //dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
        dialog.setCancelable(true);// 设置是否可以通过点击Back键取消  
        dialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条  
        dialog.setIcon(R.drawable.ic_launcher);//  
        // 设置提示的title的图标,默认是没有的,如果没有设置title的话只设置Icon是不会显示图标的  
        dialog.setTitle("提示");  
        // dismiss监听  
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {  

            @Override  
            public void onDismiss(DialogInterface dialog) {  
                // TODO Auto-generated method stub  

            }  
        });  
        // 监听Key事件被传递给dialog  
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {  

            @Override  
            public boolean onKey(DialogInterface dialog, int keyCode,  
                    KeyEvent event) {  
                // TODO Auto-generated method stub  
                return false;  
            }  
        });  
        // 监听cancel事件  
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {  

            @Override  
            public void onCancel(DialogInterface dialog) {  
                // TODO Auto-generated method stub  

            }  
        });  
        //设置可点击的按钮,最多有三个(默认情况下)  
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",  
                new DialogInterface.OnClickListener() {  

                    @Override  
                    public void onClick(DialogInterface dialog, int which) {  
                        // TODO Auto-generated method stub  

                    }  
                });  
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",  
                new DialogInterface.OnClickListener() {  

                    @Override  
                    public void onClick(DialogInterface dialog, int which) {  
                        // TODO Auto-generated method stub  

                    }  
                });  
        dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",  
                new DialogInterface.OnClickListener() {  

                    @Override  
                    public void onClick(DialogInterface dialog, int which) {  
                        // TODO Auto-generated method stub  

                    }  
                });  
        dialog.setMessage("这是一个圆形进度条");  
        dialog.show(); 

//下面的操作最好在线程中做
// 更新进度条的进度,可以在子线程中更新进度条进度  
dialog.incrementProgressBy(1);  
dialog.incrementSecondaryProgressBy(10)//二级进度条更新方式 

//进度条不用时就删掉它.
dialog.dismiss(); 
dialog.cancel();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值