ProgressDialog
ポジティブボタンAsyncTask
を使用した例の実装:AlertDialog
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to go to AsyncTask!")
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
new MyAsyncTaskClass().execute();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
// Here is the start of the AsyncTask
class MyAsyncTaskClass extends AsyncTask<String, String, Void> {
private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
protected void onPreExecute() {
progressDialog.setMessage("Dialog Message");
progressDialog.show();
progressDialog.setCanceledOnTouchOutside(false);
}
@Override
protected Void doInBackground(String... params) {
// TODO stuff
}
protected void onPostExecute(Void v) {
this.progressDialog.dismiss();
}
}
}