Make things happen in order in android
Im having trouble making things happen in order instead of together in android.
For instance when a user clicks a Button
, I want a ProgressDialog
to be displayed first, then dismissed, and then TextView
changed. Would I need to开发者_运维百科 make a method for each of these or is it something i can implement to make this happen.
Thanks for anyone who helps.
You can use an AsyncTask to accomplish this. Override doInBackground
and use publishProgress
and onProgressUpdate
to update your progress bar. Then, in onPostExecute
, update your TextView.
An example (without the publishProgress
and onProgressUpdate
):
mTextView = (TextView) findViewById(R.id.text1);
new AsyncTask<Void, Integer, Void>() {
private ProgressDialog mProgressDialog;
@Override
protected Void doInBackground(Void... arg0) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
mTextView.setText("All Done");
mProgressDialog.dismiss();
}
@Override
protected void onPreExecute() {
mProgressDialog = ProgressDialog.show(Welcome.this, "Progress title", "Progress message", true);
}
}.execute((Void) null);
You could create an OnDismissListener and set it on your ProgressDialog
using ProgressDialog#setOnDismissListener(DialogInterface.OnDismissListener)
-- See ProgressDialog#setOnDismissListener
精彩评论