0
@Override
    public void onBackPressed() {
    // TODO Auto-generated method stub            
      PopIt("Exit Application", "Are you sure you want to exit?");

        super.onBackPressed();

    }

    public void PopIt( String title, String message ){
            new AlertDialog.Builder(this)
            .setTitle( title )
            .setMessage( message )
            .setPositiveButton("YES", new OnClickListener() 
            {
                public void onClick(DialogInterface arg0, int arg1) {
                    //do stuff onclick of YES
                    finish();
                }
            }).setNegativeButton("NO", new OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    //do stuff onclick of CANCEL
                    Toast.makeText(getBaseContext(), "You touched CANCEL", Toast.LENGTH_SHORT).show();
                }
            }).show();
        }

此警报对话框消失得如此之快,以至于我无法阅读或单击它!这是为什么 ?

4

6 回答 6

5

问题是你在调用 super.onBackPressed(); 之前抛出了 Popit。删除它 :)

于 2012-11-27T11:06:23.983 回答
2
super.onBackPressed();

删除此行。

如果您不删除它,将触发默认功能,这将关闭当前活动。

于 2012-11-27T11:06:00.747 回答
1

试试这个

 public void onBackPressed() {
    // TODO Auto-generated method stub     

 super.onBackPressed();       
      PopIt("Exit Application", "Are you sure you want to exit?");



    }
于 2012-11-27T11:06:05.353 回答
1

试试这个

@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
    .setTitle("Really Exit?")
    .setMessage("Are you sure you want to exit?")
    .setNegativeButton(android.R.string.no, null)
    .setPositiveButton(android.R.string.yes, new OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1) {
            youractivity.super.onBackPressed();
        }
    }).create().show();
}
于 2012-11-27T11:07:47.177 回答
1

删除此行super.onBackPressed();

您正在 onBackPressed() 中显示对话框

所以它就像你是 onBackPressed(); 的压倒一切的行为;

所以不要调用超类方法。

于 2012-11-27T11:10:28.900 回答
1

去除那个super.onBackPressed();

这将起作用:

public void onBackPressed() {
    // TODO Auto-generated method stub          
      PopIt("Exit Application", "Are you sure you want to exit?");
    }
于 2012-11-27T11:11:37.607 回答