首先要配置開發環境,這很簡單,只需到官網下載 Sencha Touch 壓縮包即可。開發工具用任何編輯器都行,記事本也可以。本文使用 MyEclipse + Tomcat,Sencha Touch 採 1.01 版本,選擇搭配伺服器是為了日後範例能與後台互動;當然也可以直接開啟本機靜態 html 檔。
<!doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>messageBox.html</title> <link rel="stylesheet" href="../../ext/resources/css/sencha-touch.css" type="text/css"> <script type="text/javascript" src="../../ext/sencha-touch.js"></script> <script type="text/javascript" src="messageBox.js"></script> </head> <body> </body> </html>
若用過 ExtJS,這段很易懂:分別引入 sencha-touch.css 樣式、sencha-touch.js 核心庫,以及範例用的 messageBox.js。
Ext.setup({
icon: '../icon.png',
tabletStartupScreen: '../tablet_startup.png',
phoneStartupScreen: '../phone_startup.png',
glossOnIcon: false,
onReady: function() {
Ext.Msg.alert('提示', '第一個 Sencha Touch 程式!');
}
});
重點在 onReady 函式,其餘可複製沿用;熟悉 ExtJS 者可在此放入其他元件測試。
可使用 Opera 手機模擬器(支援 HTML5)或 Google Chrome,於瀏覽器輸入 https://localhost:8080/SenchaTouchTest/myExample/messageBox/messageBox.html 即可,也可直接開啟 html 檔。