开发者

Android WebVIew WebViewClient

开发者 https://www.devze.com 2023-04-06 21:34 出处:网络
I have a webView in my application. When a person loads a url, the webpage is loaded in the browser and not in my application as the options menu is the default and not what I have assigned. How can I

I have a webView in my application. When a person loads a url, the webpage is loaded in the browser and not in my application as the options menu is the default and not what I have assigned. How can I stop this and make it load in my webview and not the browser?

I tried webViewClient but it doesn't seem to work.

public class webView extends Activity {
    
    WebView myWebView;
    String url;
      
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.getSettings().setJavaScriptEnabled(tru开发者_JAVA百科e);
        url = "http://d.android.com";
        myWebView.setWebViewClient(new WebViewClient()       
        {
             @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) 
            {
                
                //url="http://google.com";
                //view.loadUrl(url);
                System.out.println("hello");
                return true;
            }
        });
        //Toast.makeText(this, "", Toast.LENGTH_SHORT);
        myWebView.loadUrl(url);
    }
/** Creteing an options menu**/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.menu, menu);
          //return true;
        // TODO Auto-generated method stub
        return super.onCreateOptionsMenu(menu);
    }


If you never want to open an URL in a browser you have to return false in shouldOverrideUrlLoading


another solution could be :

myWebView.setWebViewClient(new WebViewClient());

by default the default web browser opens and loads the destination URL, to overwrite this behavior, call

webView.setWebViewClient(new WebViewClient());
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号