2011-05-04 5 views
0

웹보기에서 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

+0

작성한 대화 상자를 반환하도록 코드를 변경하는 방법을 명확히 할 수 있다고 생각하십니까? 나는 아주 새롭고 많이 감사 할 것입니다! 감사! –

+0

'AlertDialog dialog = builder.create(); 라인을 변경해야합니다. dialog.show();'return builder.create();'; – Michael

답변

0

는 onKeyDown에를 통해 그 일을 대신하여 onBackPressed 버튼을 재정의하는 시도

여기 내 코드입니다. 또한 Pixie가 작성한 주석을 구현하십시오 ...