简体   繁体   English

ProgressBar setVisibility在正确的时间显示

[英]ProgressBar setVisibility doesn't show at the right time

public void generateNumbers(){
    progressBar.setVisibility(View.VISIBLE);

    while(){
        //approximately 5 second long procedure
    }
    printNumbers();
    progressBar.setVisibility(View.GONE);
}

I want that the progress bar is visible when this long procedure in while loop is running and then dissapear. 我希望在while循环中运行此长过程然后消失后,进度条可见。 The code like this shown progressBar after the loop is finished and then also dissapear (setVisiblity(GONE)). 这样的代码在循环完成后显示了progressBar,然后消失了(setVisiblity(GONE))。

In while loop i don't have any new threads or AsyncTask 在while循环中,我没有任何新线程或AsyncTask

Use an asynctask like this 使用这样的asynctask

    public class asyncTask extends AsyncTask<String, Void, String> {

        private Context context;
        private ProgressDialog dialog;

        public asyncTask(Context cxt) {
            context = cxt;

            dialog = new ProgressDialog(context);
        }



        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            dialog.setTitle("Please Wait...");
            dialog.show();



        }


        @Override
        protected void onPostExecute(String result) {
            dialog.dismiss();
        }

        @Override
        protected String doInBackground(String... arg0) {

            //Put your code here 
             }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM