how to set different title for alert dialog when WebView page is loaded?

我们两清 提交于 2019-11-28 08:13:59

问题


I want to set different title for alert dialog when WebView page is loaded but its not working.

here is the code snippet:

final AlertDialog.Builder alert = new AlertDialog.Builder(
        mContext);
// alert.setTitle("Loading...");
final WebView wv = new WebView(mContext);

wv.loadUrl("http://10.0.51.133/androidview/");
wv.getSettings().setJavaScriptEnabled(true);
wv.setVerticalScrollBarEnabled(false);

WebViewClientLoader loader= new WebViewClientLoader(alert);
wv.setWebViewClient(loader);
wv.setWebViewClient(new WebViewClient() {

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        alert.setTitle("Loading...");
        super.onPageFinished(view, url);
    }

    @Override
    public void onPageStarted(WebView view, String url,
            Bitmap favicon) {
        // TODO Auto-generated method stub
        alert.setTitle("Finished");
        super.onPageStarted(view, url, favicon);
    }
});

private class webviewclient extends WebViewClient{

}

wv.loadUrl("file:///android_asset/Like.html");
alert.setView(wv);

alert.show();

回答1:


Its perfectly work..i am posting after checking-------

public class MyActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WebView web=new WebView(this);
    web.setWebViewClient(new WebViewClient(){

        @Override
        public void onLoadResource(WebView view, String url) {
            super.onLoadResource(view, url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            alert.setTitle("Pages Finished");
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            alert.setTitle("Pages Started");
        }

    });
    web.setWebChromeClient(new WebChromeClient(){

    });
    web.loadUrl("http://www.google.com");
    builder=new AlertDialog.Builder(this);
    builder.setView(web);
    builder.setTitle("Loading...");
    alert=builder.create();
    alert.show();
}
AlertDialog alert;
Builder builder;
@Override
protected void onDestroy() {
    super.onDestroy();

}

@Override
protected void onPause() {
    super.onPause();
}

}

and then it changes to




回答2:


You just have use the method setCustomTitle, when create os start to load the webview.

Then, when onLoadCompleted, you can setCustomTitle again to the second one.

Here is the reference




回答3:


I think before you call alert.show(), you should call alert.create(). This should solve your problem.




回答4:


See this Example: THIS LINK

After implementing that you have to set the different value for the alert title based on the execution. So It shows the different Dialog based on the Webview is loading.

See this example for how it works and how to implement it.



来源:https://stackoverflow.com/questions/8634319/how-to-set-different-title-for-alert-dialog-when-webview-page-is-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!