【问题标题】:How to detect back button event?如何检测返回按钮事件?
【发布时间】:2013-05-22 14:12:27
【问题描述】:

navigator.notification.confirm 打开时如何检测返回按钮事件?后退按钮只是关闭弹窗,但是事件 document.addEventListener('backbutton', onBackKeyDown, false);没有提出。

【问题讨论】:

  • 我添加了一个Phonegap标签,因为这主要是Phonegap问题。此外,这不是批评,但不要指望没有代码示例的解决方案,即使它是分类的。

标签: jquery cordova jquery-mobile


【解决方案1】:

在您传递给navigator.notification.confirm API 的success 回调中,您可以获得单击哪个按钮的buttonIndex。它没有记录,但如果 buttonIndex=0,则用户要么单击对话框外部以关闭它,要么单击“返回”按钮。

例如:

  function makeConfirm(){
        navigator.notification.confirm(
            'You are the winner!',  // message
            onConfirm,              // callback to invoke with index of button pressed
            'Game Over',            // title
            'Restart,Exit'          // buttonLabels
        );        
    }

    function onConfirm(buttonIndex){
        console.log("confirmation! Button clicked was:" + buttonIndex);
        if( buttonIndex===0 ){
            // they either hit back button or tapped the area outside of the dialog
        }
    }

我不确定是否有办法确定他们是单击了实际的硬件后退按钮还是只是单击了对话框。您可以在页面正文上注册一个点击事件,看看它是否被触发,如果触发了,buttonIndex===0,然后他们在对话框外点击。如果buttonIndex===0 没有被触发,他们点击了硬件返回按钮。

我还阅读了有关使用 JQM 检测是否按下后退按钮的 StackOverflow 上的其他问题;也许你可以用这个。

【讨论】:

  • navigator.notification.confirm 处于活动状态时,它不会用于处理返回按钮事件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-05
  • 1970-01-01
  • 2015-04-25
  • 2016-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多