Appcelerator:关闭所有文本字段的键盘,而不知道哪个是打开的

时间:2017-04-10 10:55:13

标签: appcelerator appcelerator-titanium

可以隐藏文本字段的键盘,而不知道哪个文本字段的焦点处于活动状态?

例如,如果我有一个视图,其中包含4个文本字段,并且按钮来隐藏此视图,我必须执行此操作一定要关闭所有键盘(在按钮的点击事件上)

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

或者在焦点/模糊事件的帮助下,在全局变量中保存当前打开的文本字段的引用。

  • 同样的问题enter image description here
  • 我发现有趣的文档:here

2 个答案:

答案 0 :(得分: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模拟器中测试并运行。

答案 1 :(得分:0)

使用Ti.Keyboard等自定义模块:

https://github.com/manojdcoder/keyboard