Appcelerator:正确的方式自动关闭键盘 [英] Appcelerator: Proper Way Automatically Dismiss Keyboard

查看:19
本文介绍了Appcelerator:正确的方式自动关闭键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Titanium 编写应用程序.我希望能够在单击文本字段之外的任何内容时自动关闭键盘.我还没有为这个问题找到一个优雅的解决方案.

I'm writing an app using Titanium. I want to be able to automatically dismiss the keyboard anytime something outside of the text field is clicked. I have yet to find an elegant solution for this issue.

我已经考虑了几件事,但仍在寻找更好的解决方案:

Couple things that I've thought about, but am still looking for a better solution:

  1. 将事件侦听器分配给视图中存在的基本上所有其他内容,并关闭键盘(使用 textField.blur()).我想避免这种情况,因为它会导致很多代码只是为了关闭键盘.此外,如果我最终向视图添加任何其他内容,我还必须向该对象添加一个单击侦听器,因此它不太易于维护.
  2. 创建一个大的透明视图,并让它占据整个屏幕.将其直接放在文本字段下方,并在其中添加一键侦听器,该侦听器将关闭键盘.这是比 #1 更好的解决方案,但仍然不是很好,因为我在让 zIndexes 正常工作时遇到了很多麻烦.就我的目的而言,它也效率低下,因为我有特定宽度和高度的视图来封装文本字段.为了代码简单起见,我使用了它们,并在整个应用程序中重复使用它们.
  3. 我已尝试为文本字段的blur"事件添加侦听器,但似乎没有正确触发.
  1. Assign event listeners to basically everything else present in the view, and dismiss the keyboard (using textField.blur()). I want to avoid this since it results in a LOT of code just to dismiss the keyboard. Also, if I end up adding anything else to the view, I'll have to add a click listener to that object as well, so it's not very maintainable.
  2. Create a large transparent view, and have it take up the entire screen. Place it directly beneath the text field and add to it one click listener on that which will dismiss the keyboard. This is a better solution than #1, but still isn't great because I've had a lot of trouble getting zIndexes to work properly. It's also inefficient for my purposes because I've got views with a specific width and height that encapsulate text fields. I've used these for the sake of code simplicity and I re-use them throughout my application.
  3. I've tried adding a listener for the "blur" event for the text field but that doesn't seem to get fired appropriately.

就是这样.我有点不知所措.zIndexing 在 iPhone 上的表现也很奇怪,我还没有在 Android 上尝试过.另外,正如我上面提到的,我使用的许多文本字段都封装在设置了宽度/高度的小视图中——所以我认为这会影响 Z-index 的功能.

That's about it. I'm sort of at a loss. The zIndexing also behaves strangely on the iPhone, and I haven't tried on Android yet. Also, as I mentioned above, many of the text fields I use are encapsulated within small views with set widths/heights-- so I think that will affect the functionality of Z-indexes.

所以根本问题是:当点击焦点文本字段之外的任何内容时,关闭键盘的最佳方法是什么?

So the root question is: What's the best way to dismiss a keyboard whenever anything outside the text field that's in focus is clicked?

推荐答案

如果我是正确的,单击事件会通过所有视图和窗口传播,因此可以修改 #1 选项以检查最底层上的单击(视图或窗口),检查其来源,然后决定做什么.

If I'm correct the click event propagates through all views and windows therefore your #1 option could be modified to check for clicks on the bottom most layer (view or window), check for its source then decide what to do.

这篇关于Appcelerator:正确的方式自动关闭键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆