Android:非键盘输入法
我正在尝试为 Android 创建一个不是传统键盘的 IME(对应于不同字母的按键行),并且我无法找到有关如何执行此操作的有用资源,因为 SDK 中的所有代码示例都使用键盘 API它是内置函数。
我在 XML 文件中设计了 IME 界面,就好像它只是应用程序中的一个 Activity,但我在他们的演示中迷失了方向,因为他们的键盘只是从不同风格的 XML 结构构建的对象。
如果有人有一个不使用传统键盘结构的开源项目的链接,或者可以简单地指导我显示我已经在 IME 窗口上构建的 UI,我想我可以解决剩下的问题。
如果您需要更多具体信息,请随时询问。 提前致谢。
Im trying to create an IME for android that is not a traditional keyboard (rows of keys corresponding to the different letters) and I am having trouble finding helpful resources on how to do so since all of the code examples in the SDK use a Keyboard API and it's built in functions.
I've designed the interface of the IME in an XML file as though it were simply an Activity within in application but I'm getting lost in their demos since their Keyboards are simply objects that are built from a different style of XML structure.
If anyone has a link to an open source project that does not use the tradional keyboard structure or could simply guide me to displaying the UI that I've already constructed onto the IME window, I think I can figure the rest out.
If you need any more specfic information, please feel free to ask.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SoftKeyboard 示例的结构实际上与您需要的类似,虽然你不知道。让我们把它分解一下 — 这是一个充当输入法服务并使用标准 XML 键盘的完整存根:
请注意,命名了两个 XML 文档。第一个
res/xml/qwerty.xml
定义布局,以便KeyboardView
类知道如何绘制键盘。但是它所膨胀的布局,
res/layout/input.xml
,由以下内容组成(简化版):这就是以声明方式创建视图所需的全部内容!创建独立的
View
与创建Activity
没有太大区别。您没有标准的活动生命周期,但这两种环境都允许您访问 XML 布局。您需要做的就是使用充气器,引用您需要的任何子视图,然后返回主视图。因此,希望您在考虑完这一点后能够扩展您的布局。
如果您的布局足够简单,您甚至不需要使用 XML 布局。如果您的输入法可以完全包含一个视图,您可以直接在
onCreateInputView
中实例化它。这是一个不使用任何 XML 的完整存根输入法服务:(当然,清单和
res/xml/method.xml
文件中的样板仍然存在。)The SoftKeyboard sample is in fact structured similarly to what you need, though you don’t know it. Let’s break it down — here’s a complete stub to act as an input method service and use the standard XML keyboard:
Note that two XML documents are named. The first,
res/xml/qwerty.xml
, defines the layout so that theKeyboardView
class knows how to draw the keyboard.But the layout which it inflates,
res/layout/input.xml
, consists of this (simplified):This is all you need to declaratively create a view! Creating a stand-alone
View
isn’t much different from creating anActivity
. You don’t have the standard activity lifecycle, but both environments give you access to XML layouts. All you need to do is use the inflater, reference any subviews you need, and then return the main view.So hopefully you’ll be able to inflate your layout after thinking about this.
You don’t even need to use XML layouts if your layout is simple enough. If your input method can consist entirely of a single view, you can just instantiate it directly in
onCreateInputView
. Here’s a complete stub input method service that doesn’t use any XML:(Of course the boilerplate in the manifest and
res/xml/method.xml
files would still be there.)这个问题很古老,但对于任何寻找更新解决方案的人来说,我刚刚在 Github 上发布了一个适用于 Android 的准系统非键盘 IME: https://github.com/jskubick/MiNKI(Minimal Non-K键盘我我)。这基本上是我两天前第一次偶然发现这个问题时希望得到的答案:-)
详细信息:
由于 StackOverflow 不赞成仅链接的答案,因此以下是如何重新创建它:
新建一个Android Studio项目。
您可以稍后重命名包和项目,但是遵循这两个步骤将增加第一次尝试时一切正常的可能性。
将以下内容添加到 AndroidManifest.xml:
不要忘记 android.view.im 的元数据...如果省略它,Android 不会将其识别为有效的 IME。
res/xml/method.xml:
请注意,我指定了 imeSubtypeMode,但没有指定区域设置或语言。我最终正在编写一个与语言无关的输入法(或者至少与使用罗马字母的语言无关),并且不想冒险鼓励其他人复制可能有一天会让某人留在英国的解决方案、加拿大或其他地方的暴力咒骂,因为未来的家长式、反乌托邦版本的 Android 毫无意义地将他们锁定在指定为“en_US”的键盘之外
res/layout/minki.xml
src/main/java/example/ime/minki/MinkiService.java:
您可能想要替换 onTouch() 中的所有内容...我只是放置了尽可能少的内容来检测以下单笔画,并省略了所有通常会在手指下方绘制路径的代码,因为它添加了大量代码并使 IME 代码本身更难以理解。
在 MinkiService 中实现 OnTouchListener() 没有什么特别的。我在 onCreateInputView() 中将其注册为侦听器。请注意我如何处理普通字母、回车键和退格键。这些都不一定是“最佳实践”……但它确实有效。
它识别的笔划(长度应至少为 50-100 DP)
src/main/java/example/ime/minki/MinkiView.java:
重要!
此示例不应被视为展示任何“最佳实践”。它的全部目的是让你克服第一个障碍,并通过让你编译的 IME 安装和工作来给你一点轻松的满足感。
我特意将其剥离到骨头,以明确哪些内容必须成为非键盘 IME 的 Android Studio 项目的一部分。假设我没有忽略任何事情,那么将这 5 件事添加到新创建的 Android Studio 项目中就应该是您需要做的所有事情才能使其正常工作。
The question is ancient, but for anyone looking for a more recent solution, I just published a barebones non-keyboard IME for Android to Github: https://github.com/jskubick/MiNKI (Minimal Non-Keyboard Ime). This is basically the answer I wish I'd had available when I first stumbled across this question 2 days ago :-)
Details:
Since StackOverflow frowns upon link-only answers, here's how to re-create it:
Create a new Android Studio project.
You can always rename the package and project later, but following these two steps will increase the likelihood of everything working on the first try.
Add the following to AndroidManifest.xml:
Don't forget the meta-data for android.view.im... if you omit it, Android won't recognize it as a valid IME.
res/xml/method.xml:
Note that I specified imeSubtypeMode, but not locale or language. I'm ultimately writing an IME that's language-agnostic (or at least, language-that-uses-the-Roman-alphabet agnostic), and didn't want to risk encouraging others to copy a solution that might someday leave someone in Britain, Canada, or elsewhere swearing violently because a future paternalistic, dystopian version of Android pointlessly locked them out of a keyboard specified as "en_US"
res/layout/minki.xml
src/main/java/example/ime/minki/MinkiService.java:
You'll probably want to replace everything in onTouch()... I just put the most minimal stuff possible to detect the following single strokes, and omitted all the code that would normally draw their path below your finger because it adds massive amounts of code and makes it harder to understand the IME code itself.
There's nothing special about implementing the OnTouchListener() in MinkiService. I register it as a listener in onCreateInputView(). Notice how I handled normal letters, the enter key, and backspace. None of this is necessarily "best practice"... but it works.
Strokes it recognizes (should be at least 50-100 DP long)
src/main/java/example/ime/minki/MinkiView.java:
Important!
This example shouldn't be taken as showing any "best practice". Its whole point is to get you past the first roadblock, and give you a little bit of easy gratification by getting an IME compiled by you to install and work.
I deliberately stripped it to the bone to make it clear which things MUST be part of an Android Studio project for a non-keyboard IME. Assuming I haven't overlooked anything, adding those 5 things to a newly-created Android Studio project SHOULD be all you need to do to get it to work.