安卓:提示用户保存更改时,后退按钮是pressed按钮、提示、用户、pressed

由网友(你若无心我便休)分享简介:我有一个包含多个用户可编辑的项目(一个EditText场的RatingBar等)的活动。我想提示用户,如果后面的/ home键是pressed并进行了更改尚未被保存。通过Android文档阅读后,好像这片code应该在的onPause方法。我试图把一个AlertDialog中的onPause但是被所示的对话框,然后立刻...

我有一个包含多个用户可编辑的项目(一个EditText场的RatingBar等)的活动。我想提示用户,如果后面的/ home键是pressed并进行了更改尚未被保存。通过Android文档阅读后,好像这片code应该在的onPause方法。我试图把一个AlertDialog中的onPause但是被所示的对话框,然后立刻眼泪就下来了,因为无所不有,从完成阻止暂停。

I have an activity that contains several user editable items (an EditText field, RatingBar, etc). I'd like to prompt the user if the back/home button is pressed and changes have been made that have not yet been saved. After reading through the android documentation, it seems like this piece of code should go in the onPause method. I've tried putting an AlertDialog in the onPause however the dialog gets shown and then immediately tears down because nothing is there to block the pause from completing.

这是我想出来的,到目前为止:

This is what I've come up with so far:

@Override
protected void onPause() {
    super.onPause();

    AlertDialog ad = new AlertDialog.Builder(this).setMessage(
            R.string.rating_exit_message).setTitle(
            R.string.rating_exit_title).setCancelable(false)
            .setPositiveButton(android.R.string.ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            // User selects OK, save changes to db
                        }
                    }).setNeutralButton(android.R.string.cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            // User selects Cancel, discard all changes
                        }
                    }).show();
}

我是在正确的轨道,或有另一种方式来完成我想在这里做?任何帮助将是巨大的!

Am I on the right track or is there another way to accomplish what I'm trying to do here? Any help would be great!

推荐答案

您是不是很正确的轨道上;你应该做的是覆盖的onkeydown()并监听返回键,然后覆盖默认的行为:

You're not quite on the right track; what you should be doing is overriding onKeyDown() and listening for the back key, then overriding the default behavior:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

如果你只支持Android 2.0的高,他们已经增加了一个 onBack pressed()您可以改用:

If you're only supporting Android 2.0 and higher, they've added an onBackPressed() you can use instead:

@Override
public void onBackPressed() {
    // do something on back.
    return;
}

这答案基本上是从的这个博客帖子。读它,如果你需要的长 presses,兼容性的支持,对于虚拟硬盘键,或支持的生如 解决方案在$ P $宗座外方传教会() 等。

This answer is essentially ripped from this blog post. Read it if you need long presses, compatibility support, support for virtual hard keys, or raw solutions like onPreIme() etc.

阅读全文

相关推荐

最新文章