3

由于后退键破坏了我的应用程序并且所有数据都将丢失,我需要拦截它以询问用户这是否真的是他想要的。

我想到了以下结构:

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  
    {    
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) 
        { 
         // ask user if he really wants to exit
         // no --> return true;
         // yes --> return super.onKeyDown(keyCode, event);
         //manually entering either of the return values works fine
        }   
    return super.onKeyDown(keyCode, event);
    }

我想使用警报对话框实现的“询问用户”部分。我现在的问题是显示了警报对话框,但是 onKeyDown 方法在显示警报对话框时运行到最后,并且在警报对话框中我不知道如何告诉系统传递正确的返回值。

我想到的完整代码是

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  
    {    
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) 
        { 

            alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("Tile");
            alertDialog.setMessage("data lost, are you sure?");

            alertDialog.setButton(-1, getString(R.string.yes), new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which) 
                {
                    return;
                    //I only can return without a boolean value here.                   }
            });

            alertDialog.setButton(-2, getString(R.string.no), new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which) 
                {
                    return;
                }
            });

            alertDialog.show();
        }   
        return super.onKeyDown(keyCode, event);
    }

谢谢。

4

2 回答 2

3

当用户按下回你的对话框出现。

onKeyDown 现已处理,因此您返回 true。

您的对话框现在显示,

当您按下是时,您想模仿后退按钮,这就是finish();做

当您按否时,您只需关闭对话框并继续活动

你会想要这个:

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event)  
{    
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) 
    { 

        alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Tile");
        alertDialog.setMessage("data lost, are you sure?");

        alertDialog.setButton(-1, getString(R.string.yes), new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                finish(); // or yourContext.finish();
                //I only can return without a boolean value here.                   
            }
        });

        alertDialog.setButton(-2, getString(R.string.no), new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                // do nothing dialog will dismiss
            }
        });

        alertDialog.show();
        return true; //meaning you've dealt with the keyevent
    }   
    return super.onKeyDown(keyCode, event);
}
于 2011-06-09T08:02:03.613 回答
0

alertDialog.show(); 应该在没有按钮的 onClick 内调用

于 2011-06-09T08:00:31.197 回答