不在新浏览器中打开webview

我正在实施一个webview。 我想在网页顶部的两个button。

我有一个垂直的线性布局,里面是一个水平布局,两个button,一个webview在水平布局外。

我只是用Java代码加载Google URL。

每次运行应用程序时,它都会在应用程序之上打开一个新的浏览器,并隐藏button。 它不显示webview上方的button。 请帮助并告诉我如何在web浏览器中加载一个URL而不打开另一个浏览器,或者如何通过打开一个本地浏览器来阻止它,以便页面加载到webview本身而不是一个新的浏览器。

谢谢大家

布局应该类似于这样的:

<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> 

你也可以参考需要帮助从股票android浏览器更改为webview ,看看你是否正确启动的url。

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

为什么? 因为webview只是打开你的“完全链接”,如果该链接redirect其他链接,android会打开默认浏览器的这个动作。

在你的例子中,如你所知,当你连接到google.com谷歌将redirect到谷歌在你的国家。 例如,如果你在中国,谷歌将会去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> 

这里是主要活动的代码:

 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); } } } 

希望这个帮助你:)

在loadUrl()之前添加下面的代码将解决这个问题,

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

WebViewClient的shouldOverrideUrlLoading()完成这项工作。 这里去了Android文档forOverrideUrlLoading,

当一个新的URL将被加载到当前的WebView中时,主机应用程序有机会接pipe控制权。 如果没有提供WebViewClient,默认情况下,WebView将要求活动pipe理器为URLselect适当的处理程序。 如果提供了WebViewClient,则返回true表示主机应用程序处理url,而返回false表示当前WebView处理url …

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

我的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> 

我的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); } 

最终的结果是:

示例图像

我试过这个。 它为我工作。 它不会打开新的窗口。 它只会打开webview页面。 它隐藏的新浏览器问窗口打开..

 private WebView webView; String strUrl="url" ; webView = (WebView) findViewById(R.id.webView1); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl(strUrl); webView.setWebViewClient(new WebViewClient()); 

在你的xml文件中添加下面的代码,其中包含webview

tools:context=".MyActivity" (你的活动的名字)

我有完全相同的问题,幸运的是,浏览了大约一个小时后,我混合了一些我发现和工作的东西。

这是代码:

 WebView webView; webView = ((WebView) rootView.findViewById(R.id.detail_area)); webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new WebViewClient()); webView.loadUrl(mItem.link); 

其中“detail_area”是我的webview,rootView是我在“主/细节stream”中select的项目,链接是我想要打开的URL。