Titanium按钮监听器自定义功能

时间:2015-09-11 19:22:52

标签: function button titanium listener

我正在创建一个简单的应用程序来使用文本字段和按钮旋转图像。 (不使用合金)

当我在eventListener上捕获事件时,我想使用两个自定义函数,但是我得到了一个奇怪的错误。

代码如下

    Titanium.UI.setBackgroundColor('#000');

var win1 = Titanium.UI.createWindow({
    title: "App Rotation",
    backgroundColor : 'orange'
});


// Create a TextField.
var textField = Ti.UI.createTextField({
    height : 35,
    top : 10,
    left : 40,
    width : 240
});

win1.add(textField);

// Create an ImageView.
var image = Ti.UI.createImageView({
    image : 'appceleratorIcon.png',
});
// Add to the parent view.
win1.add(image);

var current_degrees = 0;

// Create a Button.
var button = Ti.UI.createButton({
    title : 'Rotate Image',
    top : '70%',
    left : '30%'
});

// Listen for click events.
button.addEventListener('click', function() {
    var txtfield_value = textField.value;
    var dialog_text;
    var dialog_title;

    if (txtfield_value >= -90 && txtfield_value <= 90) {

         current_degrees = rotate_degrees(current_degrees, txtfield_value);
        dialog_text = "Rotated " + txtfield_value;
        dialog_title = "Rotation completed";

    } else {

        dialog_text = "Type between -90 and 90";
        dialog_title = "Error!";

    }
     show_dialog(dialog_text, dialog_title);
});

// Add to the parent view.
win1.add(button);

function rotate_degrees(new_degrees, old_degrees) {

    var matrix2d = Ti.UI.create2DMatrix().rotate(new_degrees);
    img.transform = matrix2d;

    return (parseInt(new_degrees) + parseInt(old_degrees));
}

win1.open();

function show_dialog (dialog_text, dialog_title) {
    var dialog = Ti.UI.createAlertDialog({
    message: dialog_text,
    ok: 'Continue',
    title: dialog_title
  });
  dialog.show();
}

错误如下:

[ERROR] :  ReferenceError: anium is not defined
[ERROR] :  File: app.js
[ERROR] :  Line: undefined
[ERROR] :  SourceId: undefined
[ERROR] :  Backtrace:
[ERROR] :  undefined

我尝试过评论代码,唯一一次出现此错误的时候是我对这两个自定义函数进行评论。

这是编写自定义函数的方式,这样当调用按钮时,我可以执行我的功能,以便程序保持“清洁”状态。而不是代码肮脏?

提前致谢。

1 个答案:

答案 0 :(得分:0)

&#39; anium&#39;未定义在其他地方看起来像拼写错误。只是搜索它,也许你会在&#34; Tit anium&#34;之间找到一些空间。某处。

你还有一个函数dialog_title,但是正在创建一个具有相同名称的局部变量,并且想要使用按钮eventlistener中的函数。