2

我有一个 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);}
        }
}
4

3 回答 3

7

这是默认行为。

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

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

在此处阅读更多相关信息。

于 2013-06-07T09:07:40.000 回答
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);
于 2013-06-07T09:15:05.213 回答
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);
}
于 2013-06-19T08:10:02.093 回答