【问题标题】:Call a function with an AlertDialog使用 AlertDialog 调用函数
【发布时间】:2012-11-28 14:07:29
【问题描述】:

我刚刚开始学习 javascript,目前正在制作一个类似记事本的小应用程序。当我保存文本时,它会保存到单独窗口上不可编辑的文本区域。

我想在我的应用中添加一个确认提醒窗口。当按下“提交”按钮时,它应该会打开一个带有两个按钮(确认、取消)的警报。

“确认”应该像提交按钮当前那样保存 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");
    }
})

【问题讨论】:

  • 您能更详细地描述您遇到的错误吗?您询问如何进行确认对话框,但是您的代码似乎与将文本写入文件时的错误有关。我不确定你在找什么。
  • 目前保存时没有出现任何错误,只是不知道如何在上面的代码中正确实现 AlertDialog 功能。

标签: javascript save titanium alert android-alertdialog


【解决方案1】:
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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 2018-07-23
    相关资源
    最近更新 更多