1

在我的 android 应用程序中,我显示了警报对话框。当我单击 OnBack 按钮时,警报对话框被取消。我的问题是当用户单击该 onBack 按钮时。我必须显示该对话框。如何处理这个问题。提前致谢。

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(); 
4

3 回答 3

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();

        }         
  };  
于 2013-02-28T11:30:42.653 回答
1

尝试这个。它不会关闭对话框onBackPress

alertbox.setCancelable (false) 

设置此对话框是否CancelableBACK key.

于 2013-02-28T11:14:22.837 回答
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;
                        }});
于 2013-02-28T12:03:59.543 回答