在android中的webview中在运行时更改URL [英] Change URL at runtime in webview in android

查看:201
本文介绍了在android中的webview中在运行时更改URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个小程序,可以在 webview 中加载特定的网站.我想密切关注 URL,如果 URL 包含xxx"字样,那么它应该导航到另一个页面.例如,如果我设置 www.example.com.我现在可以导航到 www.example.com 的任何页面.如果我的网址包含像xxx"这样的词,那么我想退出该网址并导航到另一个网址.有可能吗?如何?谢谢.

I have created a small program that will load particular website in webview. I want to keep watch on URL and if URL contains 'xxx' word then it should navigate to another page. for example if I set www.example.com. and I can now navigate to any page of www.example.com. If my Url contains word like 'xxx' then I want to exit that url and navigate to another url. Is is possible? and how? Thanks.

//Here is my code.
public class MainActivity extends Activity {
private WebView Browser;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Browser = (WebView) findViewById(R.id.webView1);


    Browser.setWebViewClient(new WebViewClient()
    {
        @Override
        public void onLoadResource(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onLoadResource(view, url);
        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            System.out.println("when you click on any interlink on webview that time you got url :-" + url);
            int len = url.length();
            if(url.contains("xxx"))
            {
                url = "http://www.example.com/xxxx";
                Toast.makeText(MainActivity.this, "URL changed.."+url, Toast.LENGTH_LONG).show();
            }    
            else
                Toast.makeText(MainActivity.this, "URL not changed.."+url, Toast.LENGTH_LONG).show();
            return super.shouldOverrideUrlLoading(view, url);
        }



    });

    Browser.getSettings().setLoadsImagesAutomatically(true);
    Browser.getSettings().setJavaScriptEnabled(true);
    Browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

    Browser.loadUrl("http://www.example.com/");
    }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

推荐答案

是的.. 您可以导航 webview 运行时.将您的逻辑放入 WebViewClient 的 onPageFinished() 方法中.

yes.. you can navigate webview runtime. Put your logic into onPageFinished() method of WebViewClient.

public void onPageFinished(WebView view, String url) {
    if (webView.getUrl().contains("xxx")) {
        URL = "http://www.example.com/xxxx";
        webView.loadUrl(URL);
    }
}

这篇关于在android中的webview中在运行时更改URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆