C:\Users\username\Desktop\CordovaProject>cordova plugin add cordova-plugin-dialogs
<button id = "dialogAlert">ALERT</button> <button id = "dialogConfirm">CONFIRM</button> <button id = "dialogPrompt">PROMPT</button> <button id = "dialogBeep">BEEP</button>>
現(xiàn)在,我們將在index.js中的 onDeviceReady函數(shù)內(nèi)部添加事件偵聽器。一旦相應(yīng)的按鈕被點(diǎn)擊,監(jiān)聽器將調(diào)用回調(diào)函數(shù)。
document.getElementById("dialogAlert").addEventListener("click", dialogAlert);
document.getElementById("dialogConfirm").addEventListener("click", dialogConfirm);
document.getElementById("dialogPrompt").addEventListener("click", dialogPrompt);
document.getElementById("dialogBeep").addEventListener("click", dialogBeep);
因?yàn)槲覀兲砑恿怂膫€(gè)事件偵聽器,在 index.js 中創(chuàng)建所有回調(diào)函數(shù)。第一個(gè)是 dialogAlert。
function dialogAlert() {
var message = "I am Alert Dialog!";
var title = "ALERT";
var buttonName = "Alert Button";
navigator.notification.alert(message, alertCallback, title, buttonName);
function alertCallback() {
console.log("Alert is Dismissed!");
}
}

function dialogConfirm() {
var message = "Am I Confirm Dialog?";
var title = "CONFIRM";
var buttonLabels = "YES,NO";
navigator.notification.confirm(message, confirmCallback, title, buttonLabels);
function confirmCallback(buttonIndex) {
console.log("You clicked " + buttonIndex + " button!");
}
}

function dialogPrompt() {
var message = "Am I Prompt Dialog?";
var title = "PROMPT";
var buttonLabels = ["YES","NO"];
var defaultText = "Default"
navigator.notification.prompt(message, promptCallback, title, buttonLabels, defaultText);
function promptCallback(result) {
console.log("You clicked " + result.buttonIndex + " button! \n" +
"You entered " + result.input1);
}
}

最后一個(gè)是對話框提示音。這是用于呼叫音頻嘟嘟聲通知。該 times 參數(shù)將設(shè)置重復(fù)發(fā)出嗶嗶聲信號的次數(shù)。
function dialogBeep() {
var times = 2;
navigator.notification.beep(times);
}