小编akh*_*3in的帖子

按下后退按钮时,对话框显示两次

在代码中,当我按下后退按钮时,对话框会显示两次.有人可以告诉我如何只进行一次对话吗?

public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK))
        {
            onBackPressed();
        }
        return super.onKeyDown(keyCode, event);
         }

public void onBackPressed()
    {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Do you want to save configuration?");
            builder.setPositiveButton
                           ("Yes", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int id)
                {
                //here saveConfiguration is boolean type    
                    if (saveConfiguration()) 

                                              {
                        dialog.dismiss();
                        finish();

                    }
                    else
                    {
                        dialog.dismiss();
                    }

                }
            });
            builder.setNegativeButton("No", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int id)
                {
                    dialog.dismiss();
                    finish();
                }
            });
            builder.show();
    } …
Run Code Online (Sandbox Code Playgroud)

android onkeydown android-alertdialog

5
推荐指数
1
解决办法
2114
查看次数

标签 统计

android ×1

android-alertdialog ×1

onkeydown ×1