2011-08-04 99 views
14

我希望通過點擊後退按鈕它將顯示一個由TextViews和一個名爲exit的按鈕組成的對話框。 點擊退出按鈕後,它應該從我的應用程序android如何在按下後退按鈕後放置一個對話框

我不喜歡這樣的問世,

@Override  
public void onBackPressed() {  
    System.out.println("hiiii"); 
    final Dialog dialog = new Dialog(this); 
    dialog.setContentView(R.layout.dialog); 

    Button exitButton = (Button) dialog.findViewById(R.id.exit); 
    System.out.println("inside dialog_started"); 
    exitButton.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      MainActivity.this.finish(); 
      dialog.dismiss(); 
     } 
    }); 
    return; 
} 

日誌貓hiiiii和「內部dialog_started」被打印出來,但對話框不來了。 如何獲取對話框上的後退按鈕單擊?

+0

我明白了。我自己解決了。 – Jyosna

+0

那麼你應該把你的代碼放在這裏。 –

回答

23
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     exitByBackKey(); 

     //moveTaskToBack(false); 

     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 

protected void exitByBackKey() { 

    AlertDialog alertbox = new AlertDialog.Builder(this) 
    .setMessage("Do you want to exit application?") 
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 

     // do something when the button is clicked 
     public void onClick(DialogInterface arg0, int arg1) { 

      finish(); 
      //close(); 


     } 
    }) 
    .setNegativeButton("No", new DialogInterface.OnClickListener() { 

     // do something when the button is clicked 
     public void onClick(DialogInterface arg0, int arg1) { 
         } 
    }) 
     .show(); 

} 
0
@Override 
     public boolean onKeyDown(int keyCode, KeyEvent event) { 
if (keyCode != KeyEvent.KEYCODE_BACK) return super.onKeyDown(keyCode, event); 

      DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { 

      public void onClick(DialogInterface dialog, int which) { 
       switch (which){ 
       case DialogInterface.BUTTON_POSITIVE: 
        //Yes button clicked 
        finish(); 
        break; 

       case DialogInterface.BUTTON_NEGATIVE: 
        //No button clicked 
        break; 
       } 
      } 
     }; 

     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener) 
      .setNegativeButton("No", dialogClickListener).show(); 





     return super.onKeyDown(keyCode, event); 
    } 
0

試試這個...

 public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 

     AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
      alertDialog.setTitle("Exit Alert"); 
      alertDialog.setIcon(R.drawable.appicon); 

      alertDialog.setMessage("Do you really want to exit the Game?"); 
      alertDialog.setButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        finish(); 
       return; 
      } }); 
      alertDialog.setButton2("No", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       return; 
      }}); 
       alertDialog.show(); 

     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 
0

這裏是另一個代碼來顯示退出消息:

public void onBackPressed() { 
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
     Menu.this); 

    // set title 
    alertDialogBuilder.setTitle("Exit"); 

    // set dialog message 
    alertDialogBuilder 
     .setMessage("Do you really want to exit?") 
     .setCancelable(false) 
     .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog,int id) { 
       // if this button is clicked, close 
       // current activity 
       Menu.this.finish(); 
      } 
      }) 
     .setNegativeButton("No",new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog,int id) { 
       // if this button is clicked, just close 
       // the dialog box and do nothing 
       dialog.cancel(); 
      } 
     }); 

     // create alert dialog 
     AlertDialog alertDialog = alertDialogBuilder.create(); 

     // show it 
     alertDialog.show(); 
    } 
2
@Override 
public void onBackPressed() { 
// TODO Auto-generated method stub 
     AlertDialog.Builder builder=new AlertDialog.Builder(mContext); 
    // builder.setCancelable(false); 
     builder.setTitle("Rate Us if u like this"); 
     builder.setMessage("Do you want to Exit?"); 
     builder.setPositiveButton("yes",new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      Toast.makeText(mContext, "Yes i wanna exit", Toast.LENGTH_LONG).show(); 

      finish(); 
     } 
    }); 
     builder.setNegativeButton("No",new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      Toast.makeText(mContext, "i wanna stay on this page", Toast.LENGTH_LONG).show(); 
      dialog.cancel(); 

     } 
    }); 
     builder.setNeutralButton("Rate",new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 

      final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object 
      try { 
       startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getPackageName()))); 
      } catch (android.content.ActivityNotFoundException anfe) { 
       startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + getPackageName()))); 
      } 

     } 
    }); 
     AlertDialog alert=builder.create(); 
     alert.show(); 
    //super.onBackPressed(); 
     } 
+0

這是最好的答案 –

0
its working exactly.... 




public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK) { 
      exitByBackKey(); 

      //moveTaskToBack(false); 

      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

    protected void exitByBackKey() { 

     AlertDialog alertbox = new AlertDialog.Builder(this) 
     .setMessage("Do you want to exit application?") 
     .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 

      // do something when the button is clicked 
      public void onClick(DialogInterface arg0, int arg1) { 

       finish(); 
       //close(); 


      } 
     }) 
     .setNegativeButton("No", new DialogInterface.OnClickListener() { 

      // do something when the button is clicked 
      public void onClick(DialogInterface arg0, int arg1) { 
          } 
     }) 
      .show(); 

    } 
6

這是一個簡單的SOLU重刑:

@Override 
public void onBackPressed() { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 

    builder.setTitle("Save Or Not"); 
    builder.setMessage("Do you want to save this? "); 
    builder.setPositiveButton("Save", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      saveResult(); 
      MyActivity.super.onBackPressed(); 
     } 
    }); 
    builder.setNegativeButton("Discard", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      MyActivity.super.onBackPressed(); 
     } 
    }); 
    builder.show(); 
} 
相關問題