Webview不会在Webview中打开链接

时间:2013-06-07 09:04:59

标签: android webview

我有一个Webview,只需单击一个按钮即可打开一个URL,但是当我在网站上打开并按下一个链接时,它会在我的默认浏览器中打开该链接。这不是我想要的。任何人都可以帮我解决这个问题吗?

这是我的web.java

public class Web extends Activity {
    private static final String LOG_TAG = "Web";
    private WebView mWebView;
    public static final String URL = "";
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.web);
        mWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(false);
        String turl = getIntent().getStringExtra(URL);
        mWebView.loadUrl(turl);
        Button btnGoback = (Button) findViewById(R.id.btngoback);

        // Listening to button event
        btnGoback.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    /**
     * Provides a hook for calling "alert" from javascript. Useful for debugging
     * your javascript.
     */
    final class MyWebChromeClient extends WebChromeClient {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            Log.d(LOG_TAG, message);
            result.confirm();
            return true;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.web, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_back:
            // Single menu item is selected do something
            // Ex: launching new activity/screen or show alert message
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);}
        }
}

3 个答案:

答案 0 :(得分:6)

这是默认行为。

要捕获所有未来点击,您需要将shouldOverrideUrlLoading设置为true的自定义WebViewClient

mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
});

详细了解here

答案 1 :(得分:0)

您需要通过覆盖WebViewClient来处理点击,如下所示:

创建自定义WebViewClient。

public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub

        view.loadUrl(url);
        return true;

    }

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

 }

并设置您的网页视图

      mWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(false);
        mWebView.setWebViewClient(new myWebClient() ); // add this before loadUrl
        String turl = getIntent().getStringExtra(URL);
        mWebView.loadUrl(turl);

答案 2 :(得分:0)

/**
 * whether handle the URL by current WebView ?
 * 
 *  true : no; false: yes
 *  
 *  default is false
 */
public boolean shouldOverrideUrlLoading(final WebView view, String url) {
    Log.d("url", url);

    view.loadUrl(url);


    return super.shouldOverrideUrlLoading(view, url);
}