element.addEventListener('event_type', function(e) { // 當事件觸發時須要執行的代碼 // 對象E 將會描述這個事件 Ti.API.info('The '+e.type+' event happened'); });
事件是能夠被javascript檢測到的一種行爲。應用程序應該能夠捕獲和處理事件。固然,你必須得爲組件添加一些監聽器。 api
例如: app
element.addEventListener('event_type', function(e) { // 當事件觸發時須要執行的代碼 // 對象E 將會描述這個事件 Ti.API.info('The '+e.type+' event happened'); });
如上所示,第一個參數(event_type)用來指定咱們正在監聽的事件類型。 第二個參數一個回調函數,當事件觸發時將會執行。 ide
每一個Ti組件都有一些特殊的時間。這些API上面都有,好比說:
click事件,swipe 事件,touchstart事件等等。具體參考API。
除了一些普通的事件外,一些組件還有其本身特殊的事件,好比說:定位服務中的方向改變和位置改變事件。而還有一些事件能夠監聽到手機的震動和搖擺,等等,你均可以參考API。
這裏列出一些對象E(傳過來的event對象)的屬性:
x,y :事件觸發時 事件的觸發點(好比說點擊操做)在View上的座標值。
globalPoint :表示事件觸發時,事件觸發點在屏幕上的點的座標,eg.(12,12)。他是一個點。(IOS特有的哦)
type:事件的類型的名稱。
source:事件源。
除了可使用匿名函數做爲回調,你還有使用正常的函數:
function doSomething(e) {
// This function will be called by multiple handlers
// The event object is accessible within this function
Ti.API.info('The '+e.type+' event happened');
}
button1.addEventListener('click', doSomething);
button2.addEventListener('click', doSomething);
提示:若是你想給一些元素設置事件監聽器的話,你必須設置其 touchEnabled 爲true,好比說:click事件。
大多數狀況下,UI組件的touchEnabled 的默認值是true,若是當你發現不響應你的事件,你能夠試着設置其屬性touchEnabled =true看是否起做用。
除了能夠監聽外,固然也能夠觸發。
someButton.fireEvent('click');
也能夠爲觸發事件傳值。
someButton.fireEvent('click', {kitchen: 'sink'});
正如你看到的,事件傳值的時候是使用json來傳的,你能夠像下面同樣使用其值:
someButton.addEventListener('click', function(e){ Ti.APP.info('The value of kitchen is '+e.kitchen); });
顯示:
[INFO] The value of kitchen is sink
除了上面一些事件,你能夠自定義事件,何時用呢???例如,當你的數據庫更新了,你能夠須要及時的展現其數據,你能夠利用事件監聽。你能夠監聽其事件,而後觸發tableView的事件。
deleteButton.addEventListener('click', function(e){ // when something happens in your app database.doDelete(e.whichRecord); // fire an event so components can react theTable.fireEvent('db_updated'); }); // ... elsewhere in your code theTable.addEventListener('db_updated', function(e){ theTable.setData(database.getCurrentRecords()); });
有時,你有個事件須要不止一個組件來監聽,你能夠設置Application級別的事件
App級別的事件是一個全局的事件, 能夠在全部的上下文(contexts), functional scopes, CommonJS modules等被訪問。
Ti.App.addEventListener。。。。
這是一些示例代碼:
deleteButton.addEventListener('click', function(e){ // when something happens in your app database.doDelete(e.whichRecord); // fire a global event so components can react Ti.App.fireEvent('db_updated'); }); // ... elsewhere in your code Ti.App.addEventListener('db_updated', function(e){ theTable.setData(database.getCurrentRecords()); someOtherComponent.doSomethingElse(); });
請你記住了,當你的App一直運行時,app級的事件是常駐內存的,除非你移除它。
示例代碼:
function doSomething(e) { // do something } deleteButton.addEventListener('click', doSomething); // ... elsewhere in your code ... deleteButton.removeEventListener('click', doSomething); });
Event |
Fired when ... |
android:back |
The back button is released |
android:home |
The home button is released |
android:search |
The search button is released |
android:camera |
The camera button is released |
android:focus |
Fired when the camera button is pressed halfway and then released. |
android:volup |
The volume-up rocker button is released |
android:voldown |
The volume-down rocker button is released |
一、事件監聽器必須定義在事件觸發以前
二、想好事件被監聽的時間,通常是越晚越好,這樣能夠提高應用的響應速度,用戶體驗要好。
三、由於全局的事件監聽器是常駐內存的,直到你取消或者APP中止運行了,這樣的話,在事件監聽器中使用的局部變量也將常駐內存。這可能會致使內存泄露,具體關於參見內存的管理和發現內存泄露 。咱們給的建議是若是全局事件觸發後,若是不須要繼續監聽,你直接移除監聽器。
Finished code(示例代碼)