Home  >  Article  >  Backend Development  >  Display of Android ProgressBar progress bar and ProgressDialog progress box DEMO_PHP tutorial

Display of Android ProgressBar progress bar and ProgressDialog progress box DEMO_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:05:29978browse

When doing mobile phone development, we often encounter some time-consuming operations. At this time, the progress bar starts to come in handy.
This demo shows the ProgressBar progress bar and ProgressDialog progress box.
1. ProgressDialog progress box, the effect is as shown in the picture:

The code is as follows:

Copy code The code is as follows:

//Progress dialog button listening
class ProssButtonListener implements OnClickListener {
@Override
public void onClick(View v ) {
               myDialog = ProgressbarDemo.show(ProgressbarDemo.this, "Progressbar title", 
                                                                                                                                                                                    public void run () { Try {
/* Write a program fragment of the background operation here* /
/* In order to obviously see the effect, it is suspended for 3 seconds as a demonstration* /
Sleep (3000) ; } Catch (Exception E) {
E.printstacktrace ();
} Finally {
// Uninstall the MyDialog object created.
myDialog.dismiss();
}
}
}.start(); /* Start running Thread*/
}
}




2. Progress bar dialog box, here two situations are used to dynamically display the progress bar scale
1. The handle method
The effect diagram is as follows:
The code is as follows:

Copy the code

The code is as follows:

//Progress bar handle button listening
class ProssBarHandleButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
progressBarHandle.setVisibility(View.VISI BLE);
myProssBarhandleText.setVisibility(View.VISIBLE);
progressBarHandle.setMax(1000);
new Thread() {
public void run() {
for(int i=0;i< =1000;){
                                                                                                                                                                                                                                                             ; 🎜>                               msg.what = 1; getData().putInt ("size", i); sleep (100);
i+=10;
} catch (Exception e) {
handler.obtainMessage(-1).sendToTar get();
                                      e.printStackTrace();                          🎜>                                                                                                                                                                                       ;

     //handle接收消息
      private Handler handler = new Handler(){

             @Override
             public void handleMessage(Message msg) {           
                 switch (msg.what) {
                 case 1:               
                     progressBarHandle.setProgress(msg.getData().getInt("size"));
                     float num = (float)progressBarHandle.getProgress()/(float)progressBarHandle.getMax();
                     int result = (int)(num*100);
                     System.out.println("progressBarHandle.getProgress()======="+progressBarHandle.getProgress());
                     myProssBarhandleText.setText(result+ "%");
                     if(progressBarHandle.getProgress()==progressBarHandle.getMax()){
                         Toast.makeText(ProgressbarDemo.this, "下载成功", 1).show();
                         progressBarHandle.setVisibility(View.GONE);
                         myProssBarhandleText.setVisibility(View.GONE);
                     }
                     break;

                 case -1:
                     Toast.makeText(ProgressbarDemo.this,"下载失败", 1).show();
                     break;
                 }
             }
         };

2、使用AsyncTask方法,效果图与handle效果一样
具体代码如下:
复制代码 代码如下:

//Progress bar synctask button monitoring
class ProssBarSyncButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
new AsyncLoader().execute ((Void) null);
}
}

@Override
// Execute
before doInBackground method is executed protected void onPreExecute(){
progressBarHandle.setVisibility(View.VISIBLE);
myProssBarhandleText.setVisibility(View.VISIBLE);
progressBarHandle.setMax(100000);
}

// Do specific time-consuming operations
protected Integer doInBackground(Void... params) {
t totalSize = 100000;
for (int i = 0; i < 100000; ) {
publishProgress(i); // Pass data to the onProgressUpdate method through push message
i+=1 0;
}
               return totalSize; progress[0]);
float num = (float)progressBarHandle.getProgress()/(float)progressBarHandle.getMax();
int result = (int)(num*100);
myProssBarhandleText.setText(result + "%" );
}

// Execute after the doInBackground method ends
result, 1).show();
myProssBarhandleText.setVisibility(View.GONE);
progressBarHandle.setVisibility(View.GONE);
}
}



Click to download the DEMO example





http://www.bkjia.com/PHPjc/327687.html

www.bkjia.comtrue
http: //www.bkjia.com/PHPjc/327687.html

TechArticleWhen doing mobile development, we often encounter some time-consuming operations. At this time, the progress bar starts to appear. Used. This demo shows the ProgressBar progress bar and ProgressDialog progress box. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn