【问题标题】:How to load webview links inside this same webview Android如何在同一个 webview Android 中加载 webview 链接
【发布时间】:2012-09-15 04:21:53
【问题描述】:

我想在没有PhoneGap的情况下构建一个Android webApplication,但我遇到了一个问题:当我点击一个链接时,它会打开android默认浏览器...:那么如何在同一个webview Android中加载webview链接?

这是我的代码:

package com.example.mobilewebview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
//import android.webkit.WebViewClient;
import android.webkit.WebSettings;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView myWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl("http://m.google.com");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

谢谢!

【问题讨论】:

    标签: android web-applications webview hyperlink


    【解决方案1】:

    您必须拦截您的WebView 可能拥有的 URL 链接,以便进行自定义实现。

    你必须为你的WebView创建一个WebViewClient,然后重写public boolean shouldOverrideUrlLoading(WebView view, String url)方法来实现这样的事情。

    示例代码:

    //web view client implementation
    private class CustomWebViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) 
        {
            //do whatever you want with the url that is clicked inside the webview.
            //for example tell the webview to load that url.
            view.loadUrl(url);
            //return true if this method handled the link event
            //or false otherwise
            return true;
        }
    }}
    
    //in initialization of the webview:
    ....
    webview.setWebViewClient(new CustomWebViewClient());
    ....
    

    告诉我这是否有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多