是否的WebView需要WebViewClient工作? [英] Does WebView need a WebViewClient to work?

查看:99
本文介绍了是否的WebView需要WebViewClient工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在经历的Andr​​oid教程,并尝试了的WebView 的例子。这是我结束了:

I was going through android tutorials and tried out the WebView example. This is what I ended up with:

WebAppActivity

public class WebAppActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WebView wv = (WebView) findViewById(R.id.webView1);
        wv.loadUrl("http://www.google.com");

    }
}

的main.xml

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

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

</LinearLayout>

而不是在页面加载在应用程序本身,一旦应用程序启动Android浏览器打开默认的页面加载在浏览器而不是应用程序

不过。当我preSS后,我回到它显示空白屏幕的应用程序的活动。

But instead of loading the page in the application itself, as soon as the application starts the default android browser opens and the page loads in the browser instead of the application. When I press back I return to the application activity which displays a blank screen.

有谁知道为什么会这样?

Does anyone know why this is happening?

编辑:

清单

<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".WebAppActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

这只是表明我已经加入了INTERNET权限

This was just to show that I have added the INTERNET permission

编辑:

当我加入 WebViewClient

wv.setWebViewClient(new WebViewClient() {});

在页面加载的应用程序。这是预期的行为?难道一个Android的WebView 要求 WebViewClient? (找不到它的任何文件)

the page loads in the application. Is this expected behaviour? Does an Android WebView require a WebViewClient? (couldn't find any documentation on it)

编辑:

我注意到,当我安装APK,其中有谷歌API的仿真器就会出现此问题。在一个普通的仿真器(没有谷歌的API),它的表现符合预期。

I noticed that this problem occurs when I install the apk in an emulator which has the Google APIs. On a normal emulator (without the Google APIs) it behaves as expected.

推荐答案

是的,你必须设置一个WebViewClient,关于overrided法shouldOverrideUrlLoading让你的WebView加载的URL在你的应用程序返回true。

Yes, you have to set a WebViewClient that returns true on the overrided method 'shouldOverrideUrlLoading' so that your webview load the url in your app.

让我知道如果你想要一个例子。

Let me know if you want an example.

修改

@Aki <一href="http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView,%20java.lang.String%29"相对=nofollow> WebViewClient.shouldOverrideUrlLoading文档

给主机应用程序有机会接管控制,当一个新的URL是关于当前的WebView加载。 如果未提供WebViewClient,默认的WebView会要求活动管理器来选择适当的处理程序网址。如果WebViewClient提供,返回true表示主机应用程序处理的URL,而返回错误的表示当前的WebView处理的URL。

Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url.

这篇关于是否的WebView需要WebViewClient工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆