Android:拦截后退键

时间:2011-06-09 07:54:40

标签: android alertdialog onkeydown

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

我想到了以下结构:

@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);
    }

谢谢,A。

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

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

相关问题