2011-06-02 14 views
8

Ignorar. Estaba abriendo la aplicación incorrecta que se instaló. Funciona maravillosamente. :)Vista web de Android con el botón Atrás, si no

Tengo el botón Atrás funcionando correctamente en mi vista web, pero me estaba preguntando algo. Cómo hacer que la vista web regrese hasta que ya no pueda, y en lugar de salir del programa, haga que abra un cuadro de diálogo que le pregunte si el usuario está seguro de que no saldrá. Aquí está mi opinión sobre el código.
Gracias por tomarse el tiempo.

archivo .java

package com.vtd.whatthe; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.AlertDialog.Builder; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.View; 
import android.widget.Toast; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 

public class WhatThe extends Activity { 
    private WebView webview; 

    /** Called when the activity is first created. */ 

    public void onBackPressed(){ 

     if (webview.isFocused() && webview.canGoBack()) { 
       webview.goBack();  
     } 
     else { 
       openMyDialog(null); 

     } 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     webview = (WebView) findViewById(R.id.webview); 
     webview.setWebViewClient(new HelloWebViewClient()); 
     webview.getSettings().setJavaScriptEnabled(true); 
     webview.setInitialScale(50); 
     webview.getSettings().setUseWideViewPort(true); 
     webview.loadUrl("http://test2.com/"); 
    } 
    private class HelloWebViewClient extends WebViewClient { 

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

      } 

    public void openMyDialog(View view) { 
     showDialog(10); 
    } 

    @Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     case 10: 
      // Create our AlertDialog 
      Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage("Are you sure you want to exit? You have unlimited guesses!") 
        .setCancelable(true) 
        .setPositiveButton("Yes", 
          new DialogInterface.OnClickListener() { 
           @Override 
           public void onClick(DialogInterface dialog, 
             int which) { 
            // Ends the activity 
            WhatThe.this.finish(); 
           } 
          }) 
        .setNegativeButton("Keep Guessing!", 
          new DialogInterface.OnClickListener() { 

           @Override 
           public void onClick(DialogInterface dialog, 
             int which) { 
            Toast.makeText(getApplicationContext(), 
              "Good Luck!", 
              Toast.LENGTH_SHORT).show(); 
           } 
          }); 

      return builder.create(); 


     } 
     return super.onCreateDialog(id); 
    } 
} 
+0

feliz de tener que trabajar. Es posible que desee leer [esta publicación del blog] (http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html) de todos modos. –

Respuesta

0

tratar el siguiente fragmento

public void back() { 
    if (history.size() > 0) { 
     history.remove(history.size() - 1); 
     if (history.size() <= 0) { 
      finish(); 
     } else { 
      setContentView(history.get(history.size() - 1)); 
     } 
    } else { 
     finish(); 
    } 
} 

@Override 
public void onBackPressed() { 
    TopNewsGroup.group.back(); 
} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     TopNewsGroup.group.back(); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 
19

El código siguiente trabajó para mí

public void onBackPressed(){ 

    if (webview.isFocused() && webview.canGoBack()) { 
      webview.goBack();  
    } 
    else { 
      super.onBackPressed(); 
      finish(); 
    } 
} 
+0

canGoBack funciona como un encanto –

+0

increíble, gracias por esta respuesta –

0

Este es el código que necesita.

public void showAlertDialog(Context context, String title, String message, Boolean status) { 
    AlertDialog.Builder adb = new AlertDialog.Builder(this); 
    adb.setTitle(R.string.alerta); 
    adb.setMessage(getResources().getString(R.string.mserror)); 
    adb.setIcon((status) ? R.drawable.close : R.drawable.alerta); 

    adb.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
     finish(); 
    } }); 

    adb.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
     } }); 

    adb.show(); 
} 

Y luego

public void onBackPressed(){ 

    if (webView.isFocused() && webView.canGoBack()) { 
     webView.goBack();  
    } 
    else { 
     showAlertDialog(Web.this, "","", false); 
    } 
} 
1

probar este

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) 
{ 
    if(event.getAction() == KeyEvent.ACTION_DOWN) 
    { 
     switch(keyCode) 
     { 
     case KeyEvent.KEYCODE_BACK: 
      if(webView.canGoBack()) 
      { 
       myWebView.goBack(); 
      } 
      else 
      { 
       finish(); 
      } 
      return true; 
     } 
    } 
    return super.onKeyDown(keyCode, event); 
} 
Cuestiones relacionadas