如何禁用一页上的 Android 后退按钮并更改为每隔一页上的退出按钮 [英] How do I disable Android Back button on one page and change to exit button on every other page

查看:23
本文介绍了如何禁用一页上的 Android 后退按钮并更改为每隔一页上的退出按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Phonegap 开发一个与我的 Drupal 站点交互的 Android 应用程序.我已经重新分配了 Android 的返回"按钮来提示用户从 Drupal 服务器注销,但是,我只想在登录页面上禁用它(出于显而易见的原因).我可以让它工作,但只有在用户注销之后,在登录页面上,该按钮才会重新分配为注销按钮.这是我到目前为止的代码:

I am developing an Android application using Phonegap that interacts with my Drupal site. I have re-assigned the Android "Back" button to prompt the user to log off from the Drupal server however, I just want it disabled on the login page (for obvious reasons). I can get it to work but only until the user logs out then once on the login page the button remains re-assigned as a logoff button. Here's the code I have so far:

   <head>
         <script>
        /* Wait until device is ready to re-assign Back button */
        document.addEventListener("deviceready", onDeviceReady, false);
        function onDeviceReady() {
            document.addEventListener("backbutton", onBackKeyPress, false);
        }
        function onBackKeyPress() {
            /* If the current page is the login page, disable the button completely (aka do nothing) */
            if ($.mobile.activePage.attr('id') == 'login_page') {
            }

            /* Else, execute log off code */
            else {
                if (confirm("Are you sure you want to logout?")) {
                    /* Here is where my AJAX code for logging off goes */
                }
                else {
                    return false;
                }
            }
        }
        </script>
</head>

问题是后退按钮没有被重新分配.当用户注销并最终返回登录页面时,我无法找到重新运行上述代码的方法.

The problem is that the Back button doesn't get re-assigned. I cannot figure out a way to re-run the above code when the user logs out and ends up back on the login page.

如果有人愿意为此提供解决方案,我将不胜感激!

If anybody is willing to provide a solution for this I will be very grateful!

推荐答案

deviceready 很重要.如果不使用,有时您会阻止返回按钮,有时则不会.通常在调试中它有效,在生产中没有.

deviceready is important. If not used, sometimes you can get blocking Back button, sometimes not. Often in debug it works, in production no.

document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        document.addEventListener("backbutton", function (e) {
            e.preventDefault();
        }, false );
}

编辑 2013-11-03常见的错误是在桌面上进行开发,排除了cordova 脚本.然后忘记包含移动版本的cordova脚本.

EDIT 2013-11-03 Common mistake is that development is performed on desktop and cordova script is excluded. One then forgets to include the cordova script for the mobile version.

这篇关于如何禁用一页上的 Android 后退按钮并更改为每隔一页上的退出按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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