Cordova 對話框插件

1. 安裝

cordova plugin add cordova-plugin-dialogs

2. index.html添加按鈕

<button id="alert">alert</button>
<button id="confirm">confirm</button>
<button id="prompt">prompt</button>
<button id="beep">beep</button>

3. index.js添加事件

有四種對話框 可以根據自己的需求選擇
document.addEventListener('deviceReady',function(){
	// alert 最普通的對話框
    document.getElementById('alert').onclick=function(){
        // 接收四個參數(消息,回調函數,標題,按鈕名稱)
        navigator.notification.alert('我是消息',function(){
            alert("我是回調函數")
        },'我是標題','我是按鈕')
    }
    
    //  confirm 帶有多個按鈕 回調函數接收一個按鈕的index索引 根據索引 用if判斷 對不同的索引做不同的操作
    document.getElementById('confirm').onclick=function(){
    	// 四個參數(消息,回調函數,標題,按鈕名的數組)
    	// 這裏的回調函數接收index參數
        navigator.notification.confirm('我是消息',function(index){
        	// 打印index
            alert(index);
            // 根據索引不同 做不同的操作
            if(index===1){
                alert("按下按鈕1")
            }else if(index===2){
                alert("按下按鈕2")
            }else if(index===3){
                alert("按下按鈕3")
            }
            
        },'我是標題',['按鈕1','按鈕2','按鈕3'])
    }
	// 和confirm類似 接收對象不同 多了默認文本
    document.getElementById('prompt').onclick=function(){
        // 接收四個參數(消息,回調函數,標題,按鈕名稱的數組,默認文本)
        navigator.notification.prompt('我是信息', function(object){
            // 接收的是一個對象 {buttonIndex:數字,input1:'字符串}
            console.log(object)
            // 打印文本內容  這裏的問題的屬性名是 input1 也不知道爲啥要加個1
            alert(object.input1)
            // 根據不同的buttonIndex打印不同的內容
            if(object.buttonIndex===1){
                alert("按下按鈕1")
            }else if(object.buttonIndex===2){
                alert("按下按鈕2")
            }else if(object.buttonIndex===3){
                alert("按下按鈕3")
            }
        }, ['標題1','標題2'], ['按鈕1','按鈕2','按鈕3'],'默認文本')
    }

	// 鈴聲提示 參數爲時間  這個比較簡單
  	document.getElementById('beep').onclick=function(){
      	navigator.notification.beep(3);
  	}
})
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章