Sometimes we need a true combobox for our projects but iPhone SDK does not have a native one (at least from what I know) and of course neither has Titanium.
So we will build one. A “true” iPhone or Ipad combobox that allows you to use the same textfield to input arbitrary text or select a value from a UIPicker element.
Updated with @CJ_Reed’s screenshot and code at the final of the tutorial.
Let’s see the video first, then we’ll get to work:
Ok, what do we need for this iPhone combobox ?
First of all we need a textField to accept input from the user. Titanium lets you set the leftButton and rightButton for this textField while constructing it. So we will take advantage of this and create a textField as it follows:
var my_combo = Titanium.UI.createTextField({
hintText:"write your name or select one",
height:40,
width:300,
top:20,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
Nothing special, a regular textField with a hint for the user that will disappear when the textField has a value.
Now we need to create the rightButton for it.
We will use a system button provided by Apple (Titanium.UI.iPhone.SystemButton.DISCLOSURE) only that we will rotate it 90 degrees to server our purpose. This is the code that creates the rightButton and the transformation applied to it.
var tr = Titanium.UI.create2DMatrix();
tr = tr.rotate(90);
var drop_button = Titanium.UI.createButton({
style:Titanium.UI.iPhone.SystemButton.DISCLOSURE,
transform:tr
});
Now that we have the rightButton as we need it, the textField constructor becomes:
var my_combo = Titanium.UI.createTextField({
hintText:"write your name or select one",
height:40,
width:300,
top:20,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED,
rightButton:drop_button,
rightButtonMode:Titanium.UI.INPUT_BUTTONMODE_ALWAYS
});
Please note the rightButtonMode:Titanium.UI.INPUT_BUTTONMODE_ALWAYS declaration, it makes this button visible all the time.
This is how it looks:
Pretty sexy, isn’t it? Well we’re not done yet.
Building the modal picker
When the user focuses on the textField, the keyboard appears – so we will have to build our picker to emulate the same behaviour and to maximize the usability of our form. For this we will need a Picker and two buttons: Done and Cancel. These two buttons will be positioned in a Toolbar, again, to emulate as good as possible the keyboard behaviour.
Let’s build everything:
var picker_view = Titanium.UI.createView({
height:251,
bottom:0
});
var cancel = Titanium.UI.createButton({
title:'Cancel',
style:Titanium.UI.iPhone.SystemButtonStyle.BORDERED
});
var done = Titanium.UI.createButton({
title:'Done',
style:Titanium.UI.iPhone.SystemButtonStyle.DONE
});
var spacer = Titanium.UI.createButton({
systemButton:Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
});
var toolbar = Titanium.UI.createToolbar({
top:0,
items:[cancel,spacer,done]
});
var picker = Titanium.UI.createPicker({
top:43
});
picker.selectionIndicator=true;
var picker_data = [
Titanium.UI.createPickerRow({title:'John'}),
Titanium.UI.createPickerRow({title:'Alex'}),
Titanium.UI.createPickerRow({title:'Marie'}),
Titanium.UI.createPickerRow({title:'Eva'}),
Titanium.UI.createPickerRow({title:'James'})
];
picker.add(picker_data);
picker_view.add(toolbar);
picker_view.add(picker);
The code is a little long but is not rocket science. Some stuff to talk about though:
Everyting is wrapped inside a view – picker_view – because we will have to animate like the keyboard does, so it’s faster to animate one element only.
The height of picker_view is the height of the toolbar (43px) + the height of the picker (208px). How do I know this? I just used a ruler
The combobox interface looks like this:
Creating the picker animation
We also need to create 2 animations: slide_in and slide_out. We will animate the bottom property of the picker_view. We will need to start with the picker_view off the screen, so we will build it with:
bottom:-251
instead of 0 as it was initially.
var slide_in = Titanium.UI.createAnimation({bottom:0});
var slide_out = Titanium.UI.createAnimation({bottom:-251});
The logic behind the animations is this:
The user focuses the textField – the keyboard appears ( it’s done by the OS , no worries here) and if the picker_view is visible we need to hide it.
The user clicks the rightButton – we need to hide the keyboard and show the picker_view.
Here is the code:
my_combo.addEventListener('focus', function() {
picker_view.animate(slide_out);
});
drop_button.addEventListener('click',function() {
picker_view.animate(slide_in);
my_combo.blur();
});
cancel.addEventListener('click',function() {
picker_view.animate(slide_out);
});
I also added the click event on the cancel button to hide the picker_view.
Filling the textField with the picker’s value
The only thing we have left is to actually put the value of the picker in the my_combo textField when the user clicks the done button and hide the picker_view.
done.addEventListener('click',function() {
my_combo.value = picker.getSelectedRow(0).title;
picker_view.animate(slide_out);
});
The getSelectedRow method of the picker is returning the selected row, and we use its title. The getSelectedRow argument is the index of the columns in the picker, and since we have only one, this is 0.
Download the project
The Resource folder of the project can be downloaded from here.
Everything is MIT licensed, but as usual, spread the word
http://cssgallery.info/making-a-combo-box-in-titanium-appcelerator-code-and-video/
分享到:
相关推荐
titanium-appcelerator-progress-view, 在 Titanium Appcelerator项目中,要使用的自定义进度视图 进度视图这是一个进度视图,显示你的应用程序的某些状态。 你可以设置加载,成功和错误状态。只使用三行代码,你就...
这个"atom-appcelerator-titanium-master"文件夹可能包含了与Atom集成的Titanium插件,使得在Atom中编写和调试Titanium应用程序变得更加便捷。 这个插件可能提供的功能包括: 1. **语法高亮**:对JavaScript、XML...
适用于Visual Studio代码的Appcelerator Titanium程序包 适用于Appcelerator Titanium构建工具和附加编辑器。安装通过浏览器点击“安装”在VS Code中打开扩展视图,输入Titanium 通过Axway选择“ Titanium”扩展名...
适用于Titanium Appcelerator的Android iBeacon库该项目是Titanium Appcelerator的模块,该模块允许支持Android的Titanium应用程序通过易于使用的Java API与iBeacons进行交互。 可以通知应用程序进入/退出iBeacon...
Google-Cloud-Messaging--Titanium-, 在 Titanium 中,Google云消息传递 Google-Cloud-Messaging--Titanium -注册带有GCM和处理发送到设备的通知的Titanium MODULE 。Android平台使用c2dm进行推送,但是因为c2dm停止...
`node-titanium-sdk`是Appcelerator Titanium SDK的一部分,允许开发者利用JavaScript编写代码,然后编译成iOS、Android以及Windows平台的应用程序。这种方式极大地提高了开发效率,因为开发者只需要掌握一种语言...
Develop fully-featured mobile applications using a hands-on approach, and get inspired to develop more Overview Walk through the development of ten different mobile applications by leveraging your ...
appcelerator-978-1-8495-1926-7 使用 Appcelerator Titanium 创建移动应用程序
适用于Appcelerator Titanium构建工具和UI。 入门 需要Atom 1.21(或更高版本)。 某些功能被实现为程序包中的服务提供程序。 安装atom-ide-ui软件包。 安装appcelerator-titanium软件包。 使用Atom程序包管理器 ...
在解压的`faster-titanium-master`文件中,你应该能找到项目的源代码、文档、示例以及相关的配置文件。通过阅读源代码和文档,你可以更深入地了解如何将这个库集成到你的项目中,以提升应用的性能。同时,参与社区...
6. **A/B测试和持续优化**:"Faster Titanium"可能支持A/B测试,让开发者可以对比不同优化策略的效果,并根据数据反馈进行持续优化。 7. **WebAssembly**:作为JavaScript的补充,WebAssembly允许运行其他语言编译...
Animator, Titanium Appcelerator的Animator helper 类 #Animator: 动画是一个类,当你使用 Titanium 上的动画时,你可以轻松地学习。##Usage: 在 app.js ( 或者其他地方) 中,调用:// Load the animator helperva
ASTM B861 ASME SB 861-2010 Titanium and Titanium Alloy
titanium-appcelerator-pull-to-refresh, 在your中,创建"pull to refresh" 视图的JavaScript方法 什么?这种方法,将帮助你在你的篓中制作视图"拖到刷新"。?要使用这里方法,你需要将这些文件包含在项目中:Ti....
在 Appcelerator Titanium 中查看对话框概括使用SKStoreReviewController从iOS的10.3 +在Ttanium应用。 还可以使用本机 Android 对话框以获得最佳奇偶校验。要求钛移动SDK 8+ iOS 10.3+ 安卓 4.4+安卓使用 import ...
【钛合金-WebClient:Appcelerator Titanium的Web服务...总的来说,Titanium-WebClient是Appcelerator Titanium框架中一个强大且灵活的工具,使得开发者可以高效地与Web服务进行通信,构建出功能丰富的跨平台移动应用。
Titanium 5.0.0示例应用程序 该示例应用程序演示了Titanium 5.0中引入的许多新API。 由于此版本非常重要,因此我们为以下应用创建了单独的示例应用程序: WatchOS 2连接性: iOS应用搜索: iOS移交: 我们正在研究...
《使用Appcelerator Titanium开发iOS版CUAC FM应用详解》 在移动应用开发领域,JavaScript以其易学性和灵活性,已经成为跨平台开发的重要工具之一。本文将深入探讨如何使用Appcelerator Titanium这一基于JavaScript...