2012-11-28 35 views
0

我剛剛開始學習JavaScript,並且我正在製作一個類似於應用程序的小記事本。當我將文本保存到單獨窗口的不可編輯文本區域時。使用AlertDialog調用函數

我想添加一個確認警報窗口到我的應用程序。當按下「提交」按鈕時,它應該打開兩個按鈕的提示(確認,取消)。

「確認」應該將textArea文本保存爲當前的提交按鈕,「取消」應取消任何操作。我設法找到一個這樣的例子,但作爲新手,我是我無法實現它沒有錯誤。 得到這個代碼:

submitButton.addEventListener("click", function (e) { 
    if (textArea.value != "") { 
     var newFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, "newFile.txt"); 

     if (!newFile.exists()) { 
      newFile.write(); 
      newFile.write(textArea.value); 
      textArea02.value = textArea.value; 
     } else { 
      var fileContent = newFile.read(); 
      var newContent = fileContent.text + " " + textArea.value; 

      newFile.write(newContent); 
      textArea02.value = newContent; 

      alert("File Saved"); 
     } 

     textArea.value = ""; 
     textArea.blur(); 

    } else { 
     alert("Enter some text to save"); 
    } 
}) 
+1

你能更好的描述你所得到的錯誤?您詢問如何進行確認對話框,但是當您將文本寫入文件時,您的代碼似乎與錯誤有關。我不確定你在找什麼。 –

+0

我目前沒有任何錯誤,當我保存時,我只是不知道如何在上面的代碼中正確實現AlertDialog函數。 – user1859281

回答

0
var alertDialog = Ti.UI.createAlertDialog({ 
    title: 'Confirm', 
    message: 'Are you sure?', 
    buttonNames: [ 'No', 'Yes' ], 
    cancel: 0 // index to the cancel button 
}); 
alertDialog.addEventListener('click', function (evt) { 
    if (evt.index /* if it's 1, they hit Yes */) { 
     alert('OK!'); 
    } 
}); 
alertDialog.show();