将选项设置为选项对话框Titanium

时间:2012-09-17 14:24:09

标签: titanium titanium-mobile options

我从服务器获取了一个JSON字符串:

genre [
 'type1',
 'type2',
 'type3',
 'type4'

我想获取数组类型中的值以添加到option_dialog.I声明的选项:

var genreArr=[];
for(var i=0;i<genre.length;i++) 
{
 genreArr.push(genre[i]));
}

然后我创建选项对话框并为其设置选项。

var option_dialog=Ti.UI.createOptionDialog({});
option_dialog.options=genreArr;

但是当点击选项对话框时,它不会'显示数组genreArr中的值。可以帮助我。谢谢

3 个答案:

答案 0 :(得分:2)

options是一个CREATION-ONLY属性,如the documentation中所述。您必须将其传递给createOptionDialog调用,如下所示。

var genre = [
    'type1',
    'type2',
    'type3',
    'type4'
];

var dialog = Ti.UI.createOptionDialog({
    options: genre,
    title: 'Pick a Genre'
});
dialog.addEventListener('click', function(evt)
{
    alert('You picked ' + genre[evt.index]);
});
dialog.show();

答案 1 :(得分:0)

您可以使用我在代码中使用的方式

   var genre = [
    'type1',
    'type2',
    'type3',
    'type4'
];

//在onclick侦听器中调用选项对话框

var dialog = Ti.UI.createOptionDialog(genre).show();

答案 2 :(得分:0)

var win = Titanium.UI.createWindow({
    title:"Prompting the Device to Vibrate",
    backgroundColor:"#FFFFFF"
});

var label = Titanium.UI.createLabel({
    text:"Your choice will appear here",
    width:"auto",
    height:"auto"
});

//The option dialog is very similar to the alert dialog in its creation and handling
var colorDialog = Titanium.UI.createOptionDialog({
    title:"What color do you like?",
    options: ["Yellow","Surprise me","No thanks"],
    cancel:2//The index in the options array that defines which of the buttons is a cancel button
});

var optionButton = Titanium.UI.createButton({
    title:"Option Dialog!",
    height:48,
    width:120,
    bottom:12
});

colorDialog.addEventListener("click",function(e){
    if(e.index < 2){
        if(e.index === 0){
            win.backgroundColor = "#FFCC00";
        }else if(e.index === 1){
            win.backgroundColor = "#329FF0";
        }

    //Access the button index via e.index
    //Use that index with e.source.buttonNames to return th button name tapped: e.source.buttonNames[e.index]
    label.text = "You tapped index " + e.index;

    }else{
        label.text = "Aww shucks...";
    }

});

optionButton.addEventListener("click",function(e){
    colorDialog.show();
});


win.add(label);
win.add(optionButton);
win.open();
相关问题