其他分享
首页 > 其他分享> > Android:拦截返回键

Android:拦截返回键

作者:互联网

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

我想到了以下结构:

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

谢谢.

解决方法:

当用户按下时,将出现对话框.

现在已经处理了onKeyDown,因此您返回true.

您的对话框现在显示,

当您按“是”时,您想模仿“后退”按钮,即finish();确实

当您按no时,您只是关闭对话框而活动继续

您会想要这样的:

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

标签:onkeydown,alertdialog,android
来源: https://codeday.me/bug/20191208/2090485.html