在新浏览器中打开webview

WIS*_*ISH 19 android webview

我正在实施webview.我想在网页顶部有两个按钮.

我有一个垂直线性布局,其中一个是水平布局,有两个按钮,一个webview在水平布局之外.

我只是在Java代码中加载Google URL.

每次运行应用程序时,它都会在应用程序顶部打开一个新浏览器,并隐藏按钮.它没有显示webview上方的按钮.请帮忙告诉我如何在不打开其他浏览器的情况下在webview中加载URL,或者如何通过打开本机浏览器来阻止它,以便页面加载到webview本身而不是新的浏览器中.

谢谢大家

hqt*_*hqt 47

雅.您必须在此类中实现WebViewClient类和Override shouldOverrideURLLoading()方法.

为什么?因为webview只是打开你的"完全链接",如果该链接重定向其他链接,android将打开此操作的默认浏览器.

在您的示例中,如您所知,当您连接到google.comGoogle时,将重定向到您所在国家/地区的Google.例如,如果你在中国,谷歌将去google.com.cn,如果在越南,将会google.com.vn.

这是我的简单示例:(你可以想象这是一个新的浏览器,:笑)

首先是布局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_height="wrap_content"
        android:layout_width="wrap_content">

        <EditText 
            android:id="@+id/url"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:hint="Input URL"/>

        <Button 
            android:id="@+id/run"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="0"
            android:text="GO"/>

    </LinearLayout>

    <WebView 
        android:id="@+id/webview"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这是主要活动的代码:

package com.basic;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class WebViewExample extends Activity{ 

    WebView webView;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);

        webView = (WebView) findViewById(R.id.webview);

        Button button = (Button) findViewById (R.id.run);
        button.setOnClickListener(new OnClickListener() {           
            @Override
            public void onClick(View v) {
                gotoPage();             
            }
        });

    }

    private void gotoPage(){

        EditText text = (EditText) findViewById(R.id.url);
        String url = text.getText().toString();

        WebSettings webSettings = webView.getSettings();
        webSettings.setBuiltInZoomControls(true);

        webView.setWebViewClient(new Callback());  //HERE IS THE MAIN CHANGE
        webView.loadUrl(url);

    }

    private class Callback extends WebViewClient{  //HERE IS THE MAIN CHANGE. 

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return (false);
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

希望这对你有所帮助:)


Sat*_*esh 21

在loadUrl()之前添加以下代码将解决此问题,

wv.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
          view.loadUrl(url);
          return true;
           }}); 
Run Code Online (Sandbox Code Playgroud)

WebViewClient中的shouldOverrideUrlLoading()执行此任务.这里是shouldOverrideUrlLoading的Android文档,

当新Web将要加载到当前WebView中时,为主机应用程序提供接管控件的机会.如果未提供WebViewClient,则默认情况下WebView将要求活动管理器为URL选择正确的处理程序.如果提供了WebViewClient,则返回true表示主机应用程序处理url,而return false表示当前WebView处理url ...

http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView,%20java.lang.String%29

  • 谢谢,它有效,那里发生了什么? (3认同)

d.d*_*lov 10

我的XML实现:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent">

<WebView
    android:background="@android:color/transparent"
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></WebView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我的Java实现:

WebView webView;
public final String GlobalUrl = "http://slashdot.org/";

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

    webView = (WebView) findViewById(R.id.webView);
    loadWebViewLoad(webView);
}

private void loadWebViewLoad(WebView webview) {
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webview.getSettings().setSupportMultipleWindows(true);
    webview.setWebViewClient(new WebViewClient());
    webview.setWebChromeClient(new WebChromeClient());
    webview.loadUrl(GlobalUrl);
}
Run Code Online (Sandbox Code Playgroud)

最终结果是:

示例图片


raj*_*ath 4

布局应该与此类似:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_height="fill_parent"
                 android:layout_width="fill_parent"
                 android:orientation="vertical">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <Button android:id="@+id/button1"
                android:layout_width="0dip"
                android:layout_height="wrap_content"                
                android:layout_gravity="top"
                android:layout_weight="1"/>
            <Button android:id="@+id/button2"
                android:layout_width="0dip"
                android:layout_height="wrap_content"                
                android:layout_gravity="top"
                android:layout_weight="1" />
        </LinearLayout>
        <WebView 
            android:id="@+id/webview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

您还可以参考需要从普通 Android 浏览器更改为 WebView 的帮助来查看您是否正确启动了 url。

  • 您是否在 `browser.loadUrl("google.com");` 之前添加了以下内容 - `browser.setWebViewClient(new WebViewClientSubClass());`? (7认同)
  • 我的猜测是(如果页面正在加载),由于 WebView 没有与之关联的客户端,系统可能会请求库存浏览器启动该 URL。 (2认同)