2016-04-26 97 views
1

我已經創建了一個webview作爲覆蓋在另一個webview上,並懷疑關於第二個webview的後退按鈕實現。我的目標是如果我點擊後退按鈕它應該來到第一個webview。這是我的XML頁面:後退按鈕功能不能正常工作

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.app.MainActivity" 
tools:showIn="@layout/activity_main"> 

<WebView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/activity_main_webview"/> 
<WebView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="45dp" 
    android:id="@+id/external_webview"> 
</WebView> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:id="@+id/ext_link" 
    android:layout_height="fill_parent"> 
    <RelativeLayout android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:background="#FFFFFF" 
     android:layout_height="45dp" 
     android:weightSum="1"> 
     <Button 
      android:id="@+id/backtonews" 
      android:layout_marginTop="7dp" 
      android:layout_width="190dp" 
      android:layout_height="30dp" 
      android:background="@drawable/button"> 
     </Button> 
    </RelativeLayout> 
</RelativeLayout> 

</FrameLayout> 

這是我的後退按鈕

news.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       exWebView.setVisibility(View.GONE); 
       mWebView.setVisibility(View.VISIBLE); 
      } 
     }); 

現在寫的。如果我點擊後退按鈕後functinality不工作和覆蓋的WebView內容的功能(第二個網頁視圖)會放大,就好像放大一樣。請引導我在我的代碼中。是否包含任何錯誤? 在此先感謝。

編輯

我發現,如果我點擊後退按鈕它會第一web視圖,但它是自動重新回來的第二web視圖和它發生得這麼快。我通過使用吐司發現了這一點。

+0

嘗試寫setOnClick後'news.bringToFront()'權。功能後 –

+0

? –

+0

是的..試試吧。或者把它寫成你的onCreate的最後一行。嘗試兩種方式,看看它是否工作? –

回答

0

如果你想處理背按按鈕點擊,那麼你應該覆蓋onKeyDown

下面是一個例子

@Override 
public boolean onKeyDown (int keyCode, KeyEvent event) { 
if ((keyCode == KeyEvent.KEYCODE_BACK)) { 

      //HERE YOU WILL HANDLE THE BACK BUTTON CLICKS 

      exWebView.setVisibility(View.GONE); 
      mWebView.setVisibility(View.VISIBLE); 
    return true; 
} 
return super.onKeyDown(keyCode, event); 
} 

希望它可以幫助

+0

我在我的應用程序本身有一個按鈕,我仍然應該使用這種方法? –

+0

沒有這是默認的後退按鈕,我以爲你嘗試它默認的一個,試試看,看看它是否有效 – Max

+0

我希望它爲我已經放置在覆蓋網頁視圖的按鈕。默認後退按鈕將退出應用程序。這就是我寫這些功能的方式。 –