Android ProgressBar进度条和ProgressDialog进度框的展示(2)


//进度条synctask按钮监听
     class ProssBarSyncButtonListener implements OnClickListener {
         @Override
         public void onClick(View v) {
              new AsyncLoader().execute((Void)null); 
         }
     }

      
      //AsyncTask 任务执行
     class AsyncLoader extends AsyncTask<Void, Integer, Integer> {
         @Override
 //         在doInBackground方法执行前执行
         protected void onPreExecute(){
             progressBarHandle.setVisibility(View.VISIBLE);
             myProssBarhandleText.setVisibility(View.VISIBLE);
             progressBarHandle.setMax(100000);
         }

 //        做具体的耗时操作
         protected Integer doInBackground(Void... params) {
             //这里以1万的进度条刻度来显示
             int totalSize = 100000;
             for (int i = 0; i < 100000; ) {
                 publishProgress(i);  //通过推送消息传递数据给onProgressUpdate方法执行
                 i+=10;
             }
             return totalSize;
         }
 //        在doInBackground方法执行过程中执行
         protected void onProgressUpdate(Integer... progress) {
             progressBarHandle.setProgress(progress[0]); 
             float num = (float)progressBarHandle.getProgress()/(float)progressBarHandle.getMax();
             int result = (int)(num*100);
              myProssBarhandleText.setText(result+ "%");
         }

 //        在doInBackground方法结束后执行
         protected void onPostExecute(Integer result) {
             Toast.makeText(ProgressbarDemo.this, "下载成功,下载了"+result, 1).show();
             myProssBarhandleText.setVisibility(View.GONE);
             progressBarHandle.setVisibility(View.GONE);
         }
     }


点击下载DEMO示例

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/ba2708cfa6fc05ce6770ece4747fc09c.html