如何禁用在一个页面上的Andr​​oid后退按钮并更改退出所有其他网页上的按钮按钮、页面、网页、oid

2023-09-05 23:17:25 作者:掌柜、我要典当爱情√

我开发一个Android应用程序中使用PhoneGap的,与我的Drupal站点交互。我已经重新分配了Android后退按钮,提示用户然而从Drupal的服务器注销,我只是希望它的登录页面(原因很明显)上禁用。我可以得到它的工作,但只有等到用户在登录页面上登出那么一旦按钮仍然重新分配为注销按钮。这里的code我到目前为止有:

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>

的问题是,返回按钮不被重新分配。我不能想出一个办法,重新运行上面的code当用户注销并最终回到登录页面。

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月3号 常见的错误是:发展是对桌面进行,科尔多瓦脚本被排除在外。 一个再忘了包括移动版的科尔多瓦脚本。

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.