2011-05-04 129 views
0

我正在创建一个在webview中打开url的应用程序。然后当后退按钮被按下时,这会打开一个对话框询问用户是否确定他们是否要退出。虽然该对话框正常工作,但该应用程序不会加载该网站,现在我有错误,因此当我放入HelloWebViewClient时甚至无法进行编译。 你有什么建议吗? 谢谢!带后退按钮的WebView对话框

这里是我的代码:

package de.vogella.android.alertdialog; 





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 ShowMyDialog extends Activity { 
    private WebView webview; 

    /** Called when the activity is first created. */ 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if ((keyCode == KeyEvent.KEYCODE_BACK)) { 
      openMyDialog(null); 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 




    @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://mdsitest2.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?") 
        .setCancelable(true) 
        .setPositiveButton("Yes", 
          new DialogInterface.OnClickListener() { 
           @Override 
           public void onClick(DialogInterface dialog, 
             int which) { 
            // Ends the activity 
            ShowMyDialog.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(); 
           } 
          }); 


      AlertDialog dialog = builder.create(); 
      dialog.show(); 

     } 
     return super.onCreateDialog(id); 
    } 
} 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 



<WebView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/webview" 
    android:layout_height="fill_parent" android:layout_width="fill_parent" android:scrollbarAlwaysDrawVerticalTrack="false"/> 




</LinearLayout> 
+0

不要从'onCreateDialog()'方法调用'dialog.show();'。只需返回创建的对话框 – Michael 2011-05-04 19:39:46

+0

你认为你可以澄清我将如何改变代码返回创建的对话框?我很新,非常感谢!谢谢! – 2011-05-05 08:30:13

+0

你必须改变行'AlertDialog dialog = builder.create();你的代码中的dialog.show();'返回builder.create();'; – Michael 2011-05-05 09:00:12

回答

0

尝试重写的onBackPressed按钮,而不是通过onkeydown事件做这件事的。还执行Pixie所做的注释...