开发者

By clicking the button the progressbar will be loaded

开发者 https://www.devze.com 2023-04-12 04:55 出处:网络
I have create the android application program.In this program i am using the progressbar..I put the code for progressbar in the xml layout. my problem is, when i click the button after that only the p

I have create the android application program.In this program i am using the progressbar..I put the code for progressbar in the xml layout. my problem is, when i click the button after that only the progreessbar is loaded ,but it will displayed before clicking the button..pls tell some idea..Thanks in advance.开发者_运维问答.


Set android:visibility and android:id attributes for ProgressBar element.

android:visibility="gone" android:id="@+id/your_prg_bar_id"

For the button click add attribute. android:onClick

android:onClick="showProgressBar"

Inside your activity write the method showProgressBar to show progressBar.

public void showProgressBar(View source){

  progressBar = (ProgressBar) findViewById(R.id.your_prg_bar_id);
  progressBar.setVisibility(View.VISIBLE);
}


try this:: AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute

    private class xyz extends AsyncTask<Void, Void, Void> {
        private final ProgressDialog dialog = new ProgressDialog(tranning.this);
        protected void onPreExecute() {
          this.dialog.setMessage("Please Wait...");
          this.dialog.show();
          // put your code which preload with processDialog  
          @Override
          protected Void doInBackground(Void... arg0) {
               // put your code here
               return null;
          }

         @Override
         protected void onPostExecute(final Void unused) {
            if (this.dialog.isShowing()) {
            this.dialog.dismiss();
         }   
      }
  }

and use this in your button click event ::

 new xyz().execute();
0

精彩评论

暂无评论...
验证码 换一张
取 消