2013-07-11 55 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。整个布局可以改进。