• Android--创建进度框ProgressDialog


    1、布局文件progress_dialog_activity.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" >
        
        <Button 
            android:id="@+id/btn_progress_dialog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_progress_dialog_text"
            android:layout_gravity="center_horizontal"/>
    </LinearLayout>

    2、Activity类ProgressDialogActivity.java

    package cn.luxh.dialognew;
    
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class ProgressDialogActivity extends Activity {
        
        private Button btn_progress_dialog;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.progress_dialog_activity);
            handleBtnProgressDialogClick();
        }
        
        /**
         * 处理按钮事件
         */
        private void handleBtnProgressDialogClick() {
            btn_progress_dialog = (Button) findViewById(R.id.btn_progress_dialog);
            btn_progress_dialog.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    createProgressDialog();
                }
            });
        }
        
        /**
         * 生成进度对话框
         */
        private void createProgressDialog() {
            //显示进度对话框
            final ProgressDialog progressDialog = ProgressDialog.show(this, "提示", "处理中..."); 
            
            //业务处理
            new Thread(new Runnable(){
                @Override
                public void run() {
                    try {
                        Thread.sleep(10000);
                        progressDialog.dismiss();//取消进度对话框
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        
        
    }

    3、运行效果


      一个带实时进度的对话框

    代码如下:

    package cn.luxh.dialognew;
    
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class ProgressDialogActivity extends Activity {
        
        private Button btn_progress_dialog;
        
        private ProgressDialog progressDialog;
        
        private static final String TAG = "ProgressDialogActivity";
        
        //控制线程中断
        private static boolean isRun = true;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.progress_dialog_activity);
            handleBtnProgressDialogClick();
        }
        
        /**
         * 处理按钮事件
         */
        private void handleBtnProgressDialogClick() {
            btn_progress_dialog = (Button) findViewById(R.id.btn_progress_dialog);
            btn_progress_dialog.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    isRun = true;
                    createProgressDialog2();
                    progressDialog.show();
                    progressDialog.setProgress(0);
                    new Thread(new Runnable(){
                        @Override
                        public void run() {
                            int i = 1;
                            while(isRun && i<=20) {
                                try {
                                    Log.d(TAG, "i is:"+i);
                                    Thread.sleep(1500);
                                    progressDialog.incrementProgressBy(100/20);
                                    i++;
                                } catch (InterruptedException e) {
                                    isRun = false;//中断线程
                                    e.printStackTrace();
                                }
                            }
                        }
                    }).start();
                }
            });
        }
        
        /**
         * 生成进度对话框
         */
        private void createProgressDialog2() {
            progressDialog = new ProgressDialog(this);
            progressDialog.setIcon(R.drawable.ic_launcher);
            progressDialog.setTitle("实时进度...");
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setCancelable(false);//设置模态窗口
            progressDialog.setButton(ProgressDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    isRun = false;//中断线程
                    Toast.makeText(getBaseContext(), "确定按钮被点击!", Toast.LENGTH_SHORT).show();
                }
            });
            progressDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    isRun = false;//中断线程
                    Toast.makeText(getBaseContext(), "取消按钮被点击!", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    运行效果:

  • 相关阅读:
    使用RoboCopy 命令[转载]
    取得超级管理员权限
    重置网络命令win7
    ASP.NET Global.asax详解【转】
    逆波兰式算法
    设计模式【转自JackFrost的博客】
    VS2013 F12无法转到函数的定义处,总是从“元数据”获取的问题 ——解决方法
    扩展方法 C#
    委托Func和Action【转】
    添加路由
  • 原文地址:https://www.cnblogs.com/luxh/p/3470616.html
Copyright © 2020-2023  润新知