安卓:拦截返回键 [英] Android: intercepting back key

查看:114
本文介绍了安卓:拦截返回键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于返回键破坏我的应用程序,所有数据都将丢失,我需要拦截询问用户,如果这是他想要的真的是什么。

since the back key destroys my application and all data will be lost I need to intercept it to ask the user if that is really what he wants.

我觉得以下结构:

@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方法,同时显示警告对话框运行到结束,并且警告对话框内我不知道如何告诉系统来传递正确的返回值。

The "ask user" piece I wanted to realize using an alert dialog. My problem is now that the alert dialog is displayed but that the onKeyDown method runs to the end while the alert dialog is shown and that within the alert dialog I have no idea how to tell the system to pass the right return value.

完整code我脑子里想的是

The complete code I had in mind is

@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。

推荐答案

当用户presses回出现在对话框。

When the user presses back your dialog appears.

中,onKeyDown现在已经处理了,所以你返回true。

The onKeyDown has now been dealt with so you return true.

您对话框现在显示,

在preSS是你想模仿后退按钮是什么的完成();确实

when you press yes you want to imitate the back button which is what finish(); does

在preSS没有你刚刚关闭该对话框和活动继续

when you press no you just dismiss the dialog and the activity continues

您会希望这样的:

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

这篇关于安卓:拦截返回键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆