Progress dialog on button click in android

2019-08-02 04:38发布

I am making an android app in which i have to use progress dialog in android.I want to load progress image when i clicked on login button.Actully i am parsing response and i want till the time the response gets parsed it should show progress dialog.Any help will be appreciated. Thanks.

2条回答
男人必须洒脱
2楼-- · 2019-08-02 04:52

Yes you can do that like code below and link. http://developer.android.com/reference/android/os/AsyncTask.html

  private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
         protected Long doInBackground(URL... urls) {
             int count = urls.length;
             long totalSize = 0;
             for (int i = 0; i < count; i++) {
                 totalSize += Downloader.downloadFile(urls[i]);
                 publishProgress((int) ((i / (float) count) * 100));
             }
             return totalSize;
         }

         protected void onProgressUpdate(Integer... progress) {
             setProgressPercent(progress[0]);
         }

         protected void onPostExecute(Long result) {
             showDialog("Downloaded " + result + " bytes");
         }
     }
查看更多
闹够了就滚
3楼-- · 2019-08-02 05:17

Show dialog box like this

ProgressDialog pd=new ProgressDialog(context);
pd.setMessage("Loading..");pd.setTitle("Please wait");
Thread thread=new Thread(new Runnable() {
    @Override
    public void run() {

            //do your parsing;  //But do not update user interface here
            handler1.sendEmptyMessage(1);
    }
});
pd.show();
thread.start();

now handler message and dismiss dialog using handler class

 private Handler handler=new Handler(){
    //Handle message here and dismiss dialog box
};
查看更多
登录 后发表回答