2013-07-11 53 views
0

手機後退按鈕在web視圖中工作,但是當我想從Webview退出到應用程序時,此操作不會執行任何操作。 我放了一個返回到應用程序的按鈕,但我沒有那樣保持。使用手機後退按鈕退出webview

JAVA:

package com.wiralss.adb; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Button; 
public class about extends Activity implements OnClickListener{ 
WebView webView; 

    final Activity activity = this; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 


super.onCreate(savedInstanceState); 
this.getWindow().requestFeature(Window.FEATURE_PROGRESS); 
setContentView(R.layout.instagrem); 
webView = (WebView) findViewById(R.id.webView1); 
Button B123=(Button)findViewById(R.id.button1235); 
B123.setOnClickListener(this); 
webView.getSettings().setJavaScriptEnabled(true); 
webView.loadUrl("https://www.google.com"); 

webView.setWebChromeClient(new WebChromeClient() { 
    public void onProgressChanged(WebView view, int progress) 
    { 
     activity.setTitle("Loading..."); 
     activity.setProgress(progress * 100); 

     if(progress == 100) 
      activity.setTitle(R.string.app_name); 

    } 
}); 


webView.setWebViewClient(new WebViewClient() { 
    @Override 
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
    { 
     // Handle the error 

    } 


    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) 
    { 
     view.loadUrl(url); 
     return true; 
    } 
}); 

    } 



@Override 
public void onClick(View v) { 
    switch(v.getId()){ 

    case R.id.button1235: 
     Intent B = new Intent(this, MainActivity.class); 
    startActivity(B); 
    break; 

    // TODO Auto-generated method stub 

} 

    // TODO Auto-generated method stub 

} 
public void onBackPressed(){ 

    if (webView.isFocused() && webView.canGoBack()) { 
      webView.goBack();  
    } 
    else { 
      super.onBackPressed(); 
      finish(); 
    } 
} 
@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     webView.goBack(); 


     return true; 
    } 
    return false; 
} 
} 

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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/button1235" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="580px" 
      android:layout_weight="0.05" 
      android:text="Button" /> 

    </LinearLayout> 

</RelativeLayout> 

另一個問題。 要刪除我需要刪除的按鈕嗎?:

Button B123=(Button)findViewById(R.id.button1); 
B123.setOnClickListener(this); 

而且?

@Override 
public void onClick(View v) { 
    switch(v.getId()){ 

    case R.id.button1: 
     Intent B = new Intent(this, MainActivity.class); 
    startActivity(B); 
    break; 

    // TODO Auto-generated method stub 

} 

    // TODO Auto-generated method stub 

} 

坦克你非常。

+0

爲什麼要調用'webView.isFocused()'? – gunar

+0

這是一個錯誤。 – omer341

+0

如果將其刪除,會發生什麼情況? – gunar

回答

3

您不需要撥打super.onBackPressed(),因爲您正在控制自己完成活動。

同樣,您並不需要方法調用。

你可以重寫你的onBackPressed方法來讀取這樣的:

public void onBackPressed() { 
    if (webView.canGoBack()) { 
     webView.goBack();  
    } else { 
     finish(); 
    } 
} 

我不是你的動機,包括你的button1235按鈕太肯定,是的東西你想要刪除?

+0

它不起作用。我想刪除按鈕,按鈕返回到應用程序。 – omer341

0

刪除onKeyDown方法實現爲您實現它的方式將案例onBackPressed永遠不會被調用。 onBackPressed應該是唯一的:

public void onBackPressed() { 

    if (webView.canGoBack()) { 
     webView.goBack(); 
    } else { 
     super.onBackPressed(); 
    } 
} 

要刪除這足以令它消失的按鈕,但首先你需要將其聲明爲類成員:

private Button B123; 

,當你在做初始化onCreate

webView = (WebView) findViewById(R.id.webView1); 
B123 = (Button) findViewById(R.id.button1235); 
B123.setOnClickListener(this); 

爲了使它消失:

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 

    case R.id.button1235: 
     B123.setVisibility(View.GONE); 
     Intent B = new Intent(this, SecondActivity.class); 
     startActivity(B); 
     break; 

    // TODO Auto-generated method stub 

    } 

    // TODO Auto-generated method stub 

} 

我會改變佈局以及你得到一些皮棉錯誤,因爲你使用px而不是dp。整個佈局可以改進。