问题
I would like to show a "loading..." message when my Android . I managed to do this with the code below, but the problem is that it keeps displaying the "Loading..." message, even when the page is already loaded. It disappears when I click on the background, but it's definitely not what I had in mind when using this code.
So what I would like to do is showing a loading message for exactly 3 seconds.
Can anyone help me out with this one?
public void onLoadResource (WebView view, String url) {
if (progressDialog == null) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
try{
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}catch(Exception exception){
exception.printStackTrace();
}
}
回答1:
Dont put an exact timer unless you sure that loading will exactly take 3secs only. If you fetching the content from the net then I will suggest you to use asynctask and in the onpreexecute() show the loading dialog and in the onpostexecute() dissmiss the dialog. this works. Else If you sure then put wait(3000) and dissmiss the dialog
回答2:
Try this way :
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
if(progress==100 && progressDialog.isShowing()){
progressDialog.dismiss();
progressDialog = null;
}
}
});
来源:https://stackoverflow.com/questions/27076506/loading-message-when-starting-android-app-doesnt-disappear