Android hourglass

前端 未结 2 658
长发绾君心
长发绾君心 2021-01-30 09:44

How can I programmatically display an hourglass in an Android application?

2条回答
  •  被撕碎了的回忆
    2021-01-30 10:11

    Here is a simple example of doing it using AsyncTask:

    public class MyActivity extends Activity {
    
        protected void onCreate(Bundle savedInstanceState) {
    
            ...
    
            new MyLoadTask(this).execute(); //If you have parameters you can pass them inside execute method
    
        }
    
        private class MyLoadTask extends AsyncTask {        
    
            private ProgressDialog dialog;
    
            public MyLoadTask(MyActivity act) {
                dialog = new ProgressDialog(act);
            }       
    
            protected void onPreExecute() {
                dialog.setMessage("Loading...");
                dialog.show();
            }       
    
            @Override
            protected String doInBackground(Object... params) {         
                //Perform your task here.... 
                //Return value ... you can return any Object, I used String in this case
    
                try {
                    Thread.sleep(6000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return(new String("test"));
            }
    
            @Override
            protected void onPostExecute(String str) {          
                //Update your UI here.... Get value from doInBackground ....
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }           
            }
        }
    

提交回复
热议问题