Android hourglass

混江龙づ霸主 提交于 2019-12-20 08:40:04

问题


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


回答1:


You can use a ProgressDialog:

ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Thinking...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();

The above code will show the following dialog on top of your Activity:

Alternatively (or additionally) you can display a Progress indicator in the title bar of your Activity.

You need to request this as a feature near the top of the onCreate() method of your Activity using the following code:

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

Then turn it on like this:

setProgressBarIndeterminateVisibility(true);

and turn it off like this:

setProgressBarIndeterminateVisibility(false);



回答2:


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 <Object,Void,String>{        

        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();
            }           
        }
    }


来源:https://stackoverflow.com/questions/2140023/android-hourglass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!