2013-05-08 66 views
0

我的情況是,我想保留/保存輸入的數據文本字段,並將我的上一個輸入數據的下拉列表,當我點擊文本字段,並且如果我關閉和打開應用程序,輸入的數據仍然在下拉列表中。例如,我在文本字段中輸入「abc123」,然後按下按鈕上的輸入,它將在文本字段的下拉列表中保存「abc123」,如果我退出應用程序並再次輸入,「abc123」仍然會在下拉列表中。保留在文本字段中輸入的數據

var win = Ti.UI.createWindow({ 
backgroundColor: 'white', 
}); 

var txtfield = Ti.UI.createTextField({ 
}); 

var btn = Ti.UI.createButton({ 
title: 'go' 
}); 

win.add(btn); 
win.add(txtfield); 
win.open(); 

回答

1

你需要的值保存到SQLite的或Ti.App.Properties。

這裏是一個工作示例:

var win = Ti.UI.createWindow({ 
    backgroundColor : 'white', 
    layout : 'vertical' 
}); 

var txtfield = Ti.UI.createTextField({ 
    width : Ti.UI.FILL, 
    height : Ti.UI.SIZE, 
    hintText : 'Enter new values...', 
    top : 0 
}); 

var picker = Ti.UI.createPicker({ 
    top : 10 
}); 

var data = (Ti.App.Properties.hasProperty('itemList')) ? Ti.App.Properties.getList('itemList') : []; 
Ti.API.info('data is ' + data); 
var m = []; 
if (data) 
data.forEach(function(item) { 

    m.push(Ti.UI.createPickerRow({title:item})); 

}); 
Ti.API.info('M is ' + m); 
if (m.length > 0) 
picker.add(m); 

var btn = Ti.UI.createButton({ 
top : 10, 
width : Ti.UI.SIZE, 
height : Ti.UI.SIZE, 
title : 'go' 
}); 

btn.addEventListener('click', function(e) { 
var s = []; 
if (Ti.App.Properties.hasProperty('itemList')) { 
    Ti.API.info('Property found'); 
    s = Ti.App.Properties.getList('itemList'); 
} 
Ti.API.info('S is ' + s); 
s.push(txtfield.value); 
Ti.App.Properties.setList('itemList', s); 
picker.add(Ti.UI.createPickerRow({title:txtfield.value})); 
txtfield.value = ''; 
}); 

win.add(btn); 
win.add(txtfield); 
win.add(picker); 
win.open(); 
+0

感謝,HINI!這是工作得很好,但如果我想代替選擇只有下拉列表,像這個圖像http://i.msdn.microsoft.com/dynimg/IC14929.png – Emmanuel 2013-05-08 03:13:17

+1

移動控制不同於桌面的,有沒有出Titanium的下拉列表,如果你想要的話,你需要從頭創建 – hini 2013-05-08 03:58:03

+0

這意味着我將不得不一一使用視圖,滾動條等。 – Emmanuel 2013-05-08 04:00:15

0

絕對推薦使用的sessionStorage或localStorage的