问题
I am making an android app in which i have to use progress dialog in android.I want to load progress image when i clicked on login button.Actully i am parsing response and i want till the time the response gets parsed it should show progress dialog.Any help will be appreciated. Thanks.
回答1:
Yes you can do that like code below and link. http://developer.android.com/reference/android/os/AsyncTask.html
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
回答2:
Show dialog box like this
ProgressDialog pd=new ProgressDialog(context);
pd.setMessage("Loading..");pd.setTitle("Please wait");
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
//do your parsing; //But do not update user interface here
handler1.sendEmptyMessage(1);
}
});
pd.show();
thread.start();
now handler message and dismiss dialog using handler class
private Handler handler=new Handler(){
//Handle message here and dismiss dialog box
};
来源:https://stackoverflow.com/questions/8149349/progress-dialog-on-button-click-in-android