Android 应用程序中内置的自定义“键盘”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3196511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom 'Keyboard' built in an application on Android
提问by OlivierM
I've been looking to create a custom keyboard for my application. At first, I started to look at the SoftKeyboard for the SDK examples, but reading the Android Developer Group led me to this post:
我一直在寻找为我的应用程序创建自定义键盘。起初,我开始查看 SDK 示例中的 SoftKeyboard,但阅读 Android Developer Group 使我看到了这篇文章:
This is really not how the input method framework is supposed to work. An IME should be a generic input facility, not for a particular application. If you need some app-specific input, you should build it into your UI rather than pushing it out to a generic IME.
这实际上不是输入法框架应该如何工作的。IME 应该是通用输入工具,而不是用于特定应用程序。如果您需要一些特定于应用程序的输入,您应该将其构建到您的 UI 中,而不是将其推送到通用 IME。
How do I build an app-specific input within the UI? I mean, is there a way to extend the Keyboardapp or something and use it only in my application?
如何在 UI 中构建特定于应用程序的输入?我的意思是,有没有办法扩展键盘应用程序或其他东西并仅在我的应用程序中使用它?
Features needed for the keyboard:
键盘所需的功能:
- Shift key to display some other keys
- Special keys like square root or PI
- etc.
- Shift 键显示其他一些键
- 特殊键,如平方根或 PI
- 等等。
PS: an ugly solution could be to make a table of ImageButton for example, but I wanted to make something clean.
PS:一个丑陋的解决方案可能是制作一张 ImageButton 表格,但我想让一些东西变得干净。
采纳答案by Julian
I'm not really sure if there's a straight-forward solution to this (to that extent that it is even possible to understand the real reason behind the original question).
我不确定是否有一个直接的解决方案(在某种程度上,甚至有可能理解原始问题背后的真正原因)。
As is quoted in the original question:
正如原始问题中引用的那样:
If you need some app-specific input, you should build it into your UI rather than pushing it out to a generic IME.
如果您需要一些特定于应用程序的输入,您应该将其构建到您的 UI 中,而不是将其推送到通用 IME。
What is meant by that, is notthat you within your app should try to build in such input features by extending or modifyingthe soft keyboard on the phone. There are somany different soft keyboards (and basically, the soft keyboard is just another app), since most phone manufacturers create their own version, and people download 3rd party keyboards (such as Swype or SwiftKey etc.), and I can't picture there being a way for you to "hack" into those to add a few buttons or whatever it is you want (which could also be a major security hole, another reason why it probably isn't possible).
什么是意思,是不是您在应用程序内应尽量通过这样的输入特性来构建延长或修改软键盘的手机上。有这么多不同的软键盘(基本上,软键盘只是另一个应用程序),因为大多数手机制造商都创建了自己的版本,人们下载了 3rd 方键盘(例如 Swype 或 SwiftKey 等),我不能想象一下,有一种方法可以让您“入侵”这些按钮以添加一些按钮或您想要的任何东西(这也可能是一个主要的安全漏洞,这是不可能的另一个原因)。
What instead the above quote suggests, is that you have to create some otherform of input besidesthe keyboard. One such example, and a very good one if I might add, is how the RealCalc Scientific Calculatorlooks:
上面引用的意思是,除了键盘之外,您还必须创建一些其他形式的输入。一个这样的例子,如果我可以补充一个非常好的例子,是RealCalc 科学计算器的外观:
Now this isn't open source, so I can only guess how the code looks like (but it shouldn't be too hard a guess either): in its simplest form, this is just a grid with lots of buttons. Each button handles the onClick event, which would mean performing some kind of action (changing the label on some other buttons, showing a menu, displaying some text in the upper label or whatever), and that's probably pretty much what's to it. And of course, the phone's soft keyboard is never displayed (since you don't need a keyboard with all those buttons (and also there aren't any input fields to write anything in)).
现在这不是开源的,所以我只能猜测代码的样子(但也不应该太难猜测):在最简单的形式中,这只是一个带有很多按钮的网格。每个按钮处理 onClick 事件,这意味着执行某种操作(更改其他按钮上的标签、显示菜单、在上标签中显示一些文本或其他任何内容),这可能就是它的主要内容。当然,手机的软键盘永远不会显示(因为您不需要带有所有这些按钮的键盘(而且没有任何输入字段可以输入任何内容))。
It all boils down to the already mentioned quote: If you need some app-specific input, you should build it into your UI. Or in other words: create buttons (and don't display the soft keyboard if you don't need it) and make things happen when you click them.
这一切都归结为已经提到的引用:如果您需要一些特定于应用程序的输入,您应该将其构建到您的 UI 中。或者换句话说:创建按钮(如果不需要,不要显示软键盘)并在单击它们时发生。
And just to have mentioned it: if you dowant to create your own IME (which I strongly believe is notthe case here), you should have a look at the following resources:
只是提到它:如果您确实想创建自己的 IME(我坚信这里不是这种情况),您应该查看以下资源:
回答by sebap123
In my humble opinion you should take a look at the beginning of reference about keyboard and keyboard view http://developer.android.com/reference/android/inputmethodservice/Keyboard.htmland http://developer.android.com/reference/android/inputmethodservice/KeyboardView.html.
以我的拙见,您应该查看有关键盘和键盘视图的参考的开头http://developer.android.com/reference/android/inputmethodservice/Keyboard.html和http://developer.android.com/reference /android/inputmethodservice/KeyboardView.html。
There you can see an example of defining keyboard using XML file. I think that this is what you are looking for.
在那里您可以看到使用 XML 文件定义键盘的示例。我认为这就是你要找的。
回答by asadnwfp
As mentioned by @sebap123 Keyboardand KeyboardViewclass are the one you need to use,
正如@sebap123 所提到的, Keyboard和KeyboardView类是您需要使用的类,
Further, for Implementation, here is a quick detailed guide.
此外,对于实施,这里是一个快速详细的指南。