2013-02-28 99 views
1

在我的Android應用程序中,我顯示了警告對話框。當我點擊OnBack按鈕時,alert對話框被cancelled.My問題是當用戶單擊onBack buton.I必須顯示dialog.how可以處理這個。提前致謝。當我點擊後退按鈕警報對話框和進度對話框關閉

AlertDialog.Builder alertbox = new AlertDialog.Builder(HomeScreenActivity.this); 
alertbox.setMessage("Please select a Favorite Business"); 
alertbox.setNeutralButton("Nearby", new DialogInterface.OnClickListener() 
    { 
    public void onClick(DialogInterface arg0, int arg1) { } }) 
    .setNegativeButton("Search",new DialogInterface.OnClickListener() 
     { public void onClick(DialogInterface dialog,int id) { } }); 
alertbox.show(); 
+0

什麼您已嘗試 – moDev 2013-02-28 11:06:28

+0

public void onBackPressed() { \t \t //你的代碼。 \t \t dialog.setCancellable(false); \t \t} – Satishreddy 2013-02-28 11:11:02

+0

顯示您的代碼。 – moDev 2013-02-28 11:11:24

回答

1

試試這個。它不會關閉該對話框onBackPress

alertbox.setCancelable (false) 

設置此對話框是否CancelableBACK key.

1

試試這個

public void onBackPressed() { 
AlertDialog.Builder alertbox = new AlertDialog.Builder(HomeScreenActivity.this); 
    alertbox.setMessage(""Please select a Favorite Business") 
     .setCancelable(false) 
     .setNuetralButton("Nearby", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 


      } 
     }) 
     .setNegativeButton("Search", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 

      } 
     }); 
alertbox.create(); 
alertbox.show(); 

     }   
    }; 
1

你需要的是這樣的:

dialog.setOnKeyListener(new OnKeyListener(){ 
@Override 
        public boolean onKey(DialogInterface dialog, int keyCode, 
          KeyEvent event) { 
         // TODO Auto-generated method stub 
         if(keyCode == KeyEvent.KEYCODE_BACK){ 
           return true; 
          } 
          return false; 
         }});