How to show progress dialog in Android?

后端 未结 16 2941
予麋鹿
予麋鹿 2020-11-28 07:40

I want to show ProgressDialog when I click on Login button and it takes time to move to another page. How can I do this?

16条回答
  •  眼角桃花
    2020-11-28 08:17

    You better try with AsyncTask

    Sample code -

    private class YourAsyncTask extends AsyncTask {
        private ProgressDialog dialog;
    
        public YourAsyncTask(MyMainActivity activity) {
            dialog = new ProgressDialog(activity);
        }
    
        @Override
        protected void onPreExecute() {
            dialog.setMessage("Doing something, please wait.");
            dialog.show();
        }
        @Override
        protected Void doInBackground(Void... args) {
            // do background work here
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
             // do UI work here
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }
    

    Use the above code in your Login Button Activity. And, do the stuff in doInBackground and onPostExecute

    Update:

    ProgressDialog is integrated with AsyncTask as you said your task takes time for processing.

    Update:

    ProgressDialog class was deprecated as of API 26

提交回复
热议问题