ProgressBar setVisibility未在合适的时间显示

时间:2013-07-05 11:44:37

标签: android visibility

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

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

我希望当while循环中的这个长过程运行然后消失时,进度条是可见的。这样的代码在循环结束后显示progressBar,然后也消失(setVisiblity(GONE))。

在while循环中我没有任何新线程或AsyncTask

1 个答案:

答案 0 :(得分:1)

使用像这样的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 
             }

}