单击后退按钮以显示吐司通知,再次单击应退出应用程序

时间:2019-06-25 06:52:15

标签: android jquery cordova toast back-button

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

我尝试了一些代码,但是似乎所有代码都无法在应用中正常工作。

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”,然后在再次单击后退按钮时应退出该应用程序。 实际结果:它不显示任何吐司通知消息。

1 个答案:

答案 0 :(得分:0)

您可以这样创建逻辑:

int isTouched = false;

public void btnClick(View view){

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

}