首页 > 解决方案 > 单击后退按钮以显示 toast 通知,再次单击它应该退出应用程序

问题描述

我正在创建我的科尔多瓦应用程序,在移动应用程序中单击默认后退按钮时,它应该显示一个烤面包机通知“再次点击以退出应用程序”,然后再次单击后退按钮它应该退出应用程序。

我尝试了一些代码,但似乎没有一个代码在应用程序中工作。

window.plugins.toast.showWithOptions(
    {
      message: "Tap again to exit the app",
      duration: 1500, // ms
      position: "bottom",
      addPixelsY: -40,  // (optional) added a negative value to move it up a bit (default 0)
      data: {'foo':'bar'} // (optional) pass in a JSON object here (it will be sent back in the success callback below)
    },
    // implement the success callback
    function(result) {
      if (result && result.event) {
        console.log("The toast was tapped or got hidden, see the value of result.event");
        console.log("Event: " + result.event); // "touch" when the toast was touched by the user or "hide" when the toast geot hidden
        console.log("Message: " + result.message); // will be equal to the message you passed in
        console.log("data.foo: " + result.data.foo); // .. retrieve passed in data here

        if (result.event === 'hide') {
          console.log("The toast has been shown");
        }
      }
    }
  );

预期结果:单击移动应用程序中的默认返回按钮时,它应该显示一个烤面包机通知“ Tap again to exit the app”,然后再次单击返回按钮它应该退出应用程序。实际结果:它不显示任何 toast 通知消息。

标签: androidjquerycordovatoastback-button

解决方案


您可以像这样创建逻辑:

int isTouched = false;

public void btnClick(View view){

  if(isButtonTouched)//exit 
  else isButtonTouched=true(and show your toast)

}


推荐阅读