2011-04-06 129 views
20

我正在實施webview。我想要在網頁頂部的兩個按鈕。不在新瀏覽器中打開webview

我有一個垂直線性佈局,裏面是一個水平佈局,兩個按鈕,一個webview在水平佈局外。

我只是用Java代碼加載Google URL。

每次運行應用程序時,它都會在應用程序之上打開一個新的瀏覽器,並且按鈕會隱藏起來。它沒有顯示webview上方的按鈕。 請幫助並告訴我如何在webview中加載一個URL而無需打開另一個瀏覽器,或者我可以通過打開一個本地瀏覽器來阻止它,以便將該頁面加載到webview本身而不是新的瀏覽器。

感謝所有

回答

3

佈局應與此類似:

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

+1

感謝烏爾reply..but我的問題依舊相同。只要我加載到web視圖的網址,它就像全新的活動或瀏覽器在應用程序上打開一樣。點擊後退按鈕,它將我帶到應用程序中,我可以看到2個按鈕和空白的webview。 – WISH 2011-04-06 09:39:19

+0

你能分享你的webview的父母活動中的代碼......只是相關的部分。此外,啓動網址的代碼。 – rajath 2011-04-06 09:45:22

+0

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

46

雅。您必須在此課程中實施WebViewClient課程和OverrideshouldOverrideURLLoading()方法。

爲什麼?因爲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); 
     } 

    } 

} 

希望這有助於你:)

+0

我必須實現WebViewClient,因爲webview.loadUrl()直接打開瀏覽器。但只有在Xoom上使用ICS(4.0.3)與Honeycomb(3.2.1)下的Acer Iconia 7'完美兼容。你有沒有聽說過這樣的事情? – 2012-05-31 00:46:25

21

添加使用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 ...

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

+3

謝謝,它的工作原理,那裏發生了什麼? – gray 2014-04-02 18:55:40

+1

我編輯了答案。 – Sathesh 2014-06-25 06:54:15

+1

切和乾淨..,謝謝 – 2014-08-30 14:13:57

1

添加下面的一行在XML文件將具有的WebView

tools:context=".MyActivity"(你的活動名稱)

2

我嘗試這樣做。它爲我工作。它不會打開新的窗口。它只會打開webview頁面。它藏身的開放新的瀏覽器窗口詢問..

private WebView webView; 
String strUrl="url" ; 

webView = (WebView) findViewById(R.id.webView1); 
webView.getSettings().setJavaScriptEnabled(true); 
webView.loadUrl(strUrl); 
webView.setWebViewClient(new WebViewClient()); 
+3

首先'setWebViewClient'然後'loadUrl' – atulkhatri 2013-12-23 13:10:33

1

我有完全相同的問題,瀏覽網頁約一小時後,幸運的是我混了一些我發現的東西,它的工作。

這是代碼:

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。

+0

這對我有用。謝謝! – 2017-07-30 06:59:30

7

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

而最終的結果是:

Example image

相關問題