質問
私はプログラムでAndroidアプリケーションに砂時計を表示することができますどのように?
解決
あなたは ProgressDialog
に使用することができます
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Thinking...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
は上記のコードは、あなたのActivity
の上に次のダイアログが表示されます
代わりに(またはそれに加えて)あなたのActivity
のタイトルバーに進捗状況インジケータを表示することができます。
機能としてこれを要求する
あなたの必要次のコードを使用して、onCreate()
のActivity
方法の最上部付近ます:
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
次に、このようにそれをオンに
setProgressBarIndeterminateVisibility(true);
と、このようにそれをオフにします:
setProgressBarIndeterminateVisibility(false);
他のヒント
ここで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();
}
}
}
所属していません StackOverflow