Sencha touch 初体验

移动开发
本文详解一个Sencha Touch的例子。Sencha Touch是使用HTML5,CSS3和JavaScript来实现的,它可以在Android,iPhone和iPad平台上使用,也可以在其他带有HTML5兼容的Web浏览器的平台上使用。

我们将在本文中讲解一个Sencha Touch的例子。

效果图:Sencha touch官方一个list的例子
效果图:Sencha touch官方一个list的例子

PS:模拟器访问本地server的地址是10.0.2.2:8080,而不是127.0.0.1:8080

1、导入touch的相关Javascript,CSS以及资源文件

2、index.html

  1.  1: <!DOCTYPE html> 
  2.  2: <html> 
  3.  3: <head> 
  4.  4:     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
  5.  5:     <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=0;" /> 
  6.  6:     <title>Sencha Touch List Example</title> 
  7.  7:     <!-- import touch css and js --> 
  8.  8:     <link rel="stylesheet" href="touch/resources/css/sencha-touch.css" type="text/css"> 
  9.  9:     <script type="text/javascript" src="touch/sencha-touch-debug.js"></script><script type="text/javascript" src="src/index.js"></script> 
  10. 10: </head> 
  11. 11: <body></body> 
  12. 12: </html> 

3、index.js

  1.  1: /**  
  2.   2:  * 前言:  
  3.   3:  * 此例子来自sencha touch的官方example  
  4.   4:  * 注释用语如有不当请见谅。  
  5.   5:  */  
  6.   6: //相信这是每个页面都是一样的  
  7.   7: Ext.setup({  
  8.   8:     tabletStartupScreen: 'tablet_startup.png',  
  9.   9:     phoneStartupScreen: 'phone_startup.png',  
  10.  10:     icon: 'icon.png',  
  11.  11:     glossOnIcon: false,  
  12.  12:     onReady : function() {  
  13.  13:         //注册一个名为“Contact”的模型,显示的字段为firstName和lastName  
  14.  14:         Ext.regModel('Contact', {  
  15.  15:             fields: ['firstName', 'lastName']  
  16.  16:         });  
  17.  17:           
  18.  18:         //定义一个对象,有点类似Android里面的BaseAdapter  
  19.  19:         var groupingBase = {  
  20.  20:             itemTpl: '<div class="contact2"><strong>{firstName}</strong> {lastName}</div>',  
  21.  21:             selModel: {//选择模型,单选;应该还有多选  
  22.  22:                 mode: 'SINGLE',  
  23.  23:                 allowDeselect: true  
  24.  24:             },  
  25.  25:             grouped: true,//分组  
  26.  26:             indexBar: true, //索引栏  
  27.  27:               
  28.  28:             //定义点击事件  
  29.  29:             onItemDisclosure: {  
  30.  30:                 scope: 'test',  
  31.  31:                 //事件处理  
  32.  32:                 handler: function(record, btn, index) {  
  33.  33:                     alert('Disclose more info for ' + record.get('firstName'));  
  34.  34:                 }  
  35.  35:             },  
  36.  36:             //数据仓库  
  37.  37:             store: new Ext.data.Store({  
  38.  38:                 model: 'Contact',//与上面注册的模型对应  
  39.  39:                 sorters: 'firstName',//排序字段  
  40.  40:                   
  41.  41:                 //分组title显示的数据源,为firstName的首字母  
  42.  42:                 getGroupString : function(record) {  
  43.  43:                     return record.get('firstName')[0];  
  44.  44:                 },  
  45.  45:                 //就是数据了  
  46.  46:                 data: [  
  47.  47:                     {firstName: 'Aaron', lastName: 'Conran'},  
  48.  48:                     {firstName: 'Ape', lastName: 'Evilias'},  
  49.  49:                     {firstName: 'Dave', lastName: 'Kaneda'},  
  50.  50:                     {firstName: 'Michael', lastName: 'Mullany'},  
  51.  51:                     {firstName: 'Abraham', lastName: 'Elias'},  
  52.  52:                     {firstName: 'Jay', lastName: 'Robinson'},  
  53.  53:                     {firstName: 'Tommy', lastName: 'Maintz'},  
  54.  54:                     {firstName: 'Ed', lastName: 'Spencer'},  
  55.  55:                     {firstName: 'Jamie', lastName: 'Avins'},  
  56.  56:                     {firstName: 'Ed', lastName: 'Spencer'},  
  57.  57:                     {firstName: 'Jamie', lastName: 'Avins'},  
  58.  58:                     {firstName: 'Aaron', lastName: 'Conran'},  
  59.  59:                     {firstName: 'Dave', lastName: 'Kaneda'},  
  60.  60:                     {firstName: 'Ape', lastName: 'Evilias'},  
  61.  61:                     {firstName: 'Dave', lastName: 'Kaneda'},  
  62.  62:                     {firstName: 'Michael', lastName: 'Mullany'},  
  63.  63:                     {firstName: 'Abraham', lastName: 'Elias'},  
  64.  64:                     {firstName: 'Jay', lastName: 'Robinson'},  
  65.  65:                     {firstName: 'Tommy', lastName: 'Maintz'},  
  66.  66:                     {firstName: 'Ed', lastName: 'Spencer'},  
  67.  67:                     {firstName: 'Jamie', lastName: 'Avins'},  
  68.  68:                     {firstName: 'Aaron', lastName: 'Conran'},  
  69.  69:                     {firstName: 'Dave', lastName: 'Kaneda'},  
  70.  70:                     {firstName: 'Michael', lastName: 'Mullany'},  
  71.  71:                     {firstName: 'Abraham', lastName: 'Elias'},  
  72.  72:                     {firstName: 'Jay', lastName: 'Robinson'},  
  73.  73:                     {firstName: 'Tommy', lastName: 'Maintz'},  
  74.  74:                     {firstName: 'Ed', lastName: 'Spencer'},  
  75.  75:                     {firstName: 'Jamie', lastName: 'Avins'},  
  76.  76:                     {firstName: 'Aaron', lastName: 'Conran'},  
  77.  77:                     {firstName: 'Dave', lastName: 'Kaneda'},  
  78.  78:                     {firstName: 'Michael', lastName: 'Mullany'},  
  79.  79:                     {firstName: 'Abraham', lastName: 'Elias'},  
  80.  80:                     {firstName: 'Jay', lastName: 'Robinson'},  
  81.  81:                     {firstName: 'Michael', lastName: 'Mullany'},  
  82.  82:                     {firstName: 'Abraham', lastName: 'Elias'},  
  83.  83:                     {firstName: 'Jay', lastName: 'Robinson'},  
  84.  84:                     {firstName: 'Zed', lastName: 'Zacharias'}  
  85.  85:                 ]  
  86.  86:             })  
  87.  87:         };  
  88.  88:    
  89.  89:         /**  
  90.  90:          * 应该是判断设备类型把  
  91.  91:          * Phone和其他类型有所不同,主要就是屏幕大小了  
  92.  92:          */  
  93.  93:         if (!Ext.is.Phone) {  
  94.  94:             new Ext.List(Ext.apply(groupingBase, {  
  95.  95:                 floating: true,  
  96.  96:                 width: 350,  
  97.  97:                 height: 370,  
  98.  98:                 centered: true,  
  99.  99:                 modal: true,  
  100. 100:                 hideOnMaskTap: false  
  101. 101:             })).show();  
  102. 102:         }  
  103. 103:         else {  
  104. 104:             new Ext.List(Ext.apply(groupingBase, {  
  105. 105:                 fullscreen: true  //全屏  
  106. 106:             }));  
  107. 107:         }  
  108. 108:     }  
  109. 109: }); 

4、部署到服务器访问就行了

文章来源:http://www.cnblogs.com/halzhang/archive/2010/11/20/1882497.html

【编辑推荐】

  1. Web移动应用的未来:使用HTML5和CSS
  2. 细数六大支持智能手机的JS框架
  3. 走进四大主流移动Web开发框架
  4. Web移动应用开发框架的8个技术构想
责任编辑:佚名 来源: 博客园
相关推荐

2011-09-05 10:20:21

Sencha ToucAPP

2011-09-02 15:18:49

Sencha Touc

2011-07-25 15:55:21

Sencha ToucHtml 5

2011-07-26 09:41:50

Sencha Touc特性HTML 5

2011-07-26 09:46:53

Sencha Touc

2011-07-25 16:21:22

Sencha touc

2011-09-05 10:39:03

Sencha Touc离线存储数据库

2011-07-25 16:41:16

Sencha Touc

2011-11-28 13:42:55

Sencha Touc组件选择器

2011-09-02 16:21:08

Sencha Touc自动生成工具

2011-09-05 12:49:59

Sencha Touc事件

2011-09-05 11:23:26

EclipseSencha Touc框架

2011-09-02 15:12:29

PhoneGapSencha Touc

2012-03-08 22:31:28

Sencha Touc

2011-09-05 14:09:06

Sencha Touc函数

2011-09-05 13:53:08

Sencha Touc列表

2011-09-05 11:27:17

Sencha Touc框架HTML5

2011-09-05 14:17:54

Sencha ToucMVC

2011-09-02 15:58:38

Sencha Touc布局

2011-09-05 13:45:25

jQuery MobiSencha Touc移动Web UI框架
点赞
收藏

51CTO技术栈公众号