【问题标题】:cant able to get selected option from OptionDialog box in Titanium Appcelerator?无法从 Titanium Appcelerator 的 OptionDialog 框中获取选定的选项?
【发布时间】:2013-11-21 15:48:49
【问题描述】:

我在 Titanium 中创建了一个 OptionDialog。我已经从动态数组中添加了这些选项列表。如何在点击对话框中的任何项目时获得特定的选项值?

var View = Ti.UI.createTextField({
height : '60dp',
width : '90%',
value : 'click here'
)};

myArray = ['Lion','Tiger','Cat','Elephant','Dog'];

var opts = {
  cancel: 2,
  options: myArray,
  selectedIndex: 2,
  destructive: 0,
};

var dialog;
View.addEventListener('click',function(){
    dialog = Ti.UI.createOptionDialog(opts).show();
});

我已经尝试过如下方法,但不起作用。

dialog.addEventListener('click',function(e){
    alert('You Clicked' + e.source.options);
});

【问题讨论】:

  • 您使用的是哪个操作系统?安卓还是ios?

标签: titanium appcelerator titanium-mobile


【解决方案1】:

如下修改你的代码

var myArray = ['Lion','Tiger','Cat','Elephant','Dog'];

var opts = {
  cancel: 2,
  options: myArray,
  selectedIndex: 2,
  destructive: 0,
};

var dialog;
View.addEventListener('click',function(){
    dialog = Ti.UI.createOptionDialog(opts);
    dialog.show();
    dialog.addEventListener('click', onSelectDialog);
});

function onSelectDialog(event){
    var selectedIndex = event.source.selectedIndex;
    //OR
    //var selectedIndex = dialog.selectedIndex();
    alert('You have selected' + myArray[selectedIndex ]);
}

希望对你有帮助

【讨论】:

  • 感谢@Anand 的帮助。
  • //OR //var selectedIndex = dialog.getSelectedIndex();
  • 你可以使用其中任何一个。这就是我添加它的原因。
【解决方案2】:

对话框事件侦听器passes the index into 提供的选项数组(在您的情况下为myArray)被选中,而不是选项本身。

假设你已经定义了你的数组,试试这个:

var myArray = ['Lion','Tiger','Cat','Elephant','Dog'];

....

dialog.addEventListener('click',function(e){
    alert('You Clicked' + myArray[e.index]);
});

这将提醒来自myArray 的选项。

【讨论】:

  • 我试过上面的代码。但以下行引发错误:“dialog.addEventListener('click',function(e){" not defined
  • 在添加监听器之前是否实例化了对话框。
  • 是的。我已经在 View 的点击事件之外正确声明了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多