2017-04-10 27 views
0

可以隱藏文本框的鍵盤,而不知道哪個文本框的焦點處於活動狀態?Appcelerator:關閉所有文本框的鍵盤而不知道哪一個打開

舉例來說,如果我有一個視圖與4個文本框按鈕隱藏這個觀點,我必須爲一定要關閉所有的鍵盤(按鈕的單擊事件做到這一點)

textfield1.blur(); 
textfield2.blur(); 
textfield3.blur(); 
textfield4.blur(); 

或者在焦點/模糊事件的幫助下,在全局變量中保存當前打開的文本字段的引用。

回答

0

使用自定義模塊像Ti.Keyboard:

https://github.com/manojdcoder/keyboard

+0

謝謝,但模塊已過時 – WhiteLine

+0

它仍然可以正常使用最新的Titanium SDK 6.0.3 – danny005

+0

您是否看過源代碼?它只是對於所提到的本機方法的包裝: ''' - (ID)隱藏:(ID)ARGS { dispatch_sync(dispatch_get_main_queue(),^ { 的UIWindow *窗口= [[UIApplication的sharedApplication] keyWindow]; UIView * topView = window.rootViewController.view; [topView endEditing:YES]; }); } – danny005

1

如果你不想通過使用有點過時的模塊來創建依賴關係,您可以試試這個:

創建一個新文件,並根據需要調用它,本例中我使用yourview.js。在它下面的代碼粘貼:

module.exports = { 
    view: Ti.UI.createView({ 
     layout: 'vertical', 
     backgroundColor: '#ddd', 
     button: Ti.UI.createButton({ title: 'blur all textfields' }), 
     textfields: [ 
      Ti.UI.createTextField({ value: 'first' }), 
      Ti.UI.createTextField({ value: 'second' }), 
      Ti.UI.createTextField({ value: 'third' }), 
      Ti.UI.createTextField({ value: 'fourth' }), 
     ] 
    }), 
    construct: function() 
    { 
     var self = this; 

     self.view.button.addEventListener('click', function(){ 
      for(var i in self.view.textfields) 
       self.view.textfields[i].blur(); 
     }); 

     // Add textfields to view 
     for(var i in self.view.textfields) 
      self.view.add(self.view.textfields[i]); 

     // Add button to view 
     self.view.add(self.view.button); 

     return self.view; 
    } 
}; 

yourview.js文件包含要在該特定視圖中顯示一切。該構造函數將增加都在一起,當你要使用你的Window對象的觀點,這將是這樣的:

var win = Ti.UI.createWindow({ 
    yourview: require('namespace/ui/yourview').construct() 
}); 

// Add your reference to the scope of the Window object 
win.add(win.yourview); 

win.open(); 

// If you want to get the value of the textfields in this scope just use it like this: 
Ti.API.info(win.yourview.textfields[0].value); 

這樣,你所擁有的一切,你在不同的文件:)

想測試並且可以在使用Ti.5.4.0 SDK的iOS 9.3模擬器中運行。

+0

謝謝你的回答!但要做到這一點,我幾乎會完全改變這個非常複雜的應用程序。在我看來,仍然有一種方法有點過於複雜,奇怪的是這個函數沒有簡單的方法。 – WhiteLine

+2

更簡單的方法是在.js文件中創建一個全局數組變量,並在其中添加文本字段。然後你可以像我在我的例子中一樣遍歷它們。這很簡單,但不是最美麗的tbh。 – Garre

+0

同意。這是我們一直遵循的方法。 Upvoted。 – Soumya

相關問題