不在新浏览器中打开webview
我正在实施webview。我想要在网页顶部的两个按钮。不在新浏览器中打开webview
我有一个垂直线性布局,里面是一个水平布局,两个按钮,一个webview在水平布局外。
我只是用Java代码加载Google URL。
每次运行应用程序时,它都会在应用程序之上打开一个新的浏览器,并且按钮会隐藏起来。它没有显示webview上方的按钮。 请帮助并告诉我如何在webview中加载一个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>
而且你还可以参考Need help changing from stock android browser to webview看到,如果你正确地启动URL。
雅。您必须在此课程中实施WebViewClient
课程和Override
shouldOverrideURLLoading()
方法。
为什么?因为webview
只是打开你的“精确链接”,如果该链接重定向其他链接,android会为此操作打开默认浏览器。
在你的例子中,如你所知,当你连接到google.com
谷歌将重定向到谷歌在你的国家。例如,如果你在中国,谷歌将会去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);
}
}
}
希望这有助于你:)
我必须实现WebViewClient,因为webview.loadUrl()直接打开浏览器。但只有在Xoom上使用ICS(4.0.3)与Honeycomb(3.2.1)下的Acer Iconia 7'完美兼容。你有没有听说过这样的事情? – 2012-05-31 00:46:25
添加使用loadURL(之前下面的代码),将解决ŧ他的问题,
wv.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}});
的shouldOverrideUrlLoading()从WebViewClient做这件工作。这里是适用于shouldOverrideUrlLoading的Android文档,
当主页应用程序有一个机会在当前WebView中加载新网址时接管控件。如果未提供WebViewClient,则默认情况下,WebView将要求活动管理器为URL选择适当的处理程序。如果提供了WebViewClient,则返回true表示主机应用程序处理url,而返回false表示当前WebView处理url ...
添加下面的一行在XML文件将具有的WebView
tools:context=".MyActivity"
(你的活动名称)
我尝试这样做。它为我工作。它不会打开新的窗口。它只会打开webview页面。它藏身的开放新的浏览器窗口询问..
private WebView webView;
String strUrl="url" ;
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(strUrl);
webView.setWebViewClient(new WebViewClient());
首先'setWebViewClient'然后'loadUrl' – atulkhatri 2013-12-23 13:10:33
我有完全相同的问题,浏览网页约一小时后,幸运的是我混了一些我发现的东西,它的工作。
这是代码:
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是在一个“主/详细信息流”我选择的项目,链接是我想打开的URL。
这对我有用。谢谢! – 2017-07-30 06:59:30
我的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);
}
而最终的结果是:
感谢乌尔reply..but我的问题依旧相同。只要我加载到web视图的网址,它就像全新的活动或浏览器在应用程序上打开一样。点击后退按钮,它将我带到应用程序中,我可以看到2个按钮和空白的webview。 – WISH 2011-04-06 09:39:19
你能分享你的webview的父母活动中的代码......只是相关的部分。此外,启动网址的代码。 – rajath 2011-04-06 09:45:22
xml与回答相同。我的Java代码是:public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); browser =(WebView)findViewById(R.id.webView); browser.loadUrl(“http://www.google.com”); \t browser.getSettings()。setJavaScriptEnabled(true); showPrevious =(Button)findViewById(R.id.showPrevious); showNext =(Button)findViewById(R.id.showNext); }); } – WISH 2011-04-06 10:16:14