【问题标题】:Webview link opens in external browserWebview 链接在外部浏览器中打开
【发布时间】:2014-07-12 00:40:41
【问题描述】:

我是制作安卓应用程序的初学者。我按照THIS 教程学习了如何制作网页视图,并在我的 Galaxy Note 2 上运行了该应用程序,没有出现任何错误,但是当应用程序打开时,用代码编写的链接想要在外部浏览器中打开。我希望该页面在应用程序的 Web 视图中可见。我该如何继续执行此操作?

MainActivity.java

    WebView browser;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // find the WebView by name in the main.xml of step 2
    browser=(WebView)findViewById(R.id.wvwMain);

    // Enable javascript
    browser.getSettings().setJavaScriptEnabled(true);

    // load a webpage
    browser.loadUrl("http://www.google.com/");

}

【问题讨论】:

  • 你能发布你的代码吗?

标签: android webview android-studio


【解决方案1】:

为了帮助您更好地理解,我将简单的浏览器应用代码留给您。

这里 SimpleBrowser 是 MainActivity。

public class SimpleBrowser extends Activity implements OnClickListener {
WebView ourBrowser;
EditText url;
Button go, go_Back, go_Forward, refresh, clr_History;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.browser_webview);

    ourBrowser = (WebView) findViewById(R.id.WVBrowser);
    /* WebView Settings pretty important */
    ourBrowser.getSettings().setJavaScriptEnabled(true);
    ourBrowser.getSettings().setLoadWithOverviewMode(true);
    ourBrowser.getSettings().setUseWideViewPort(true);

    ourBrowser.setWebViewClient(new ourViewClient());
    try {
        ourBrowser.loadUrl("http://www.mybringback.com");
    } catch (Exception e) {
        e.printStackTrace();
    }

    go = (Button) findViewById(R.id.btn_Go);
    go_Back = (Button) findViewById(R.id.btn_GoBack);
    go_Forward = (Button) findViewById(R.id.btn_GoForward);
    refresh = (Button) findViewById(R.id.btn_RefreshPage);
    clr_History = (Button) findViewById(R.id.btn_ClrHistory);
    url = (EditText) findViewById(R.id.eT_webbrowser);

    go.setOnClickListener(this);
    go_Back.setOnClickListener(this);
    go_Forward.setOnClickListener(this);
    refresh.setOnClickListener(this);
    clr_History.setOnClickListener(this);

}

@Override
public void onClick(View view) {
    switch (view.getId()) {
    case R.id.btn_Go:
        String newWebaddress = url.getText().toString();
        ourBrowser.loadUrl(newWebaddress);

        /* Hiding the keyboard after the EditText data */
        InputMethodManager ipmm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        ipmm.hideSoftInputFromWindow(url.getWindowToken(), 0);
        break;

    case R.id.btn_GoBack:
        if (ourBrowser.canGoBack()) {
            ourBrowser.goBack();
        }
        break;

    case R.id.btn_GoForward:
        if (ourBrowser.canGoForward()) {
            ourBrowser.goForward();
        }
        break;

    case R.id.btn_RefreshPage:
        ourBrowser.reload();
        break;

    case R.id.btn_ClrHistory:
        ourBrowser.clearHistory();
        break;
    }
}

}
然后有另一个名称为:ourViewClient.java 的 Java 文件,它应该包含以下代码:

public class ourViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);

        return true; // as mentioned in below notes, for your case., you do 'return false'
    }

}

Android 清单文件: 确保声明了<uses-permission android:name="android.permission.INTERNET" />browser_webview.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/eT_webbrowser"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/btn_Go"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="Go" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_GoBack"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="Go back a Page"
            android:textSize="@dimen/mediumsize" />

        <Button
            android:id="@+id/btn_GoForward"
            android:layout_width="78dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:text="Go Forward"
            android:textSize="@dimen/mediumsize" />

        <Button
            android:id="@+id/btn_RefreshPage"
            android:layout_width="66dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.90"
            android:text="Refresh Page"
            android:textSize="@dimen/mediumsize" />

        <Button
            android:id="@+id/btn_ClrHistory"
            android:layout_width="82dp"
            android:layout_height="wrap_content"
            android:text="Clear History"
            android:textSize="@dimen/mediumsize" />
    </LinearLayout>

    <WebView
        android:id="@+id/WVBrowser"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </WebView>

</LinearLayout>

【讨论】:

  • 你能看看我发布的代码,看看我能不能用它做些什么。我是个菜鸟,对此我不太了解:)
  • @Stumpp - 我已经为你编辑了我的整个代码。请查看并让我们知道是否还有其他问题。我试图尽可能清楚地帮助你。希望你继续前进。
  • 非常感谢!添加代码后我有两个错误。在browser.setWebViewClient(new ourViewClient()); 行的我的主要活动java 中有一条错误消息:WebView 中的setWebViewClient(android.webkit.WebViewClient) 无法应用于(play.test2.app.ourViewClient)。同样在 ourViewClient.java 中,@override 代码出现错误:“方法不会覆盖其超类中的方法”
  • @Stumpp - 看看我的代码并使用它。我的代码应该可以帮助您回答所有问题
  • 完美!现在一切都说得通了,而且奏效了。感谢您的帮助
【解决方案2】:

你只需用这个来解决这个问题:

        myWebView.setWebViewClient(new HelloWebViewClient());
...
private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("yoursite.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}

【讨论】:

    【解决方案3】:

    在你的 oncreate() 中添加这个:

    browser.setWebViewClient(new WebViewClient()); //open urls inside browser
    

    【讨论】:

      猜你喜欢
      • 2018-02-11
      • 2017-08-06
      • 1970-01-01
      • 2015-12-09
      • 2012-09-24
      • 2019-11-23
      • 2021-05-21
      • 2021-06-12
      相关资源
      最近更新 更多