如何在Android键盘中包含建议 [英] How to include suggestions in Android Keyboard
问题描述
我正在使用Android SoftKeyboard.我已经为键盘创建了布局,但是不知道如何添加建议,如果我们在EditText中键入一些单词会出现这些建议.
例如,如果我写"Kn",则建议"中将显示已知"和已知".
所以我的问题是-
1):如何在Android软键盘中包含建议?
2)是否可以包含我们自己的建议列表?
非常感谢.
我已经检查了此和
I am working on Android SoftKeyboard. I've created layout for keyboard but dont't know how to include suggestions which appears if we type some word in EditText.
For example if i write "Kn" then "Known" and "Known" are shown in Suggestions.
So my questions are -
1) How to include suggestions in Android Softkeyboard?
2) Is there any way to include our own list of suggestions?
Thanx a lot in advance.
I've already checked this and this but not able to find any proper answer. Any help would be appreciated.
编辑
我想在键盘正上方包含建议,如下图所示.
EDIT
I want to include suggestions directly above Keyboard as shown in picture below.
推荐答案
您可以使用静态方法UserDictionary.Words.addWord(....)
:
You can use the static method UserDictionary.Words.addWord(....)
: Link
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
// On JellyBean & above, you can provide a shortcut and an explicit Locale
UserDictionary.Words.addWord(this, "MadeUpWord", 10, "Mad", Locale.getDefault());
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
UserDictionary.Words.addWord(this, "MadeUpWord", 10, UserDictionary.Words.LOCALE_TYPE_CURRENT);
}
您将需要将此权限添加到清单中
You will need to add this permission to your manifest:
<uses-permission android:name="android.permission.WRITE_USER_DICTIONARY"/>
添加的单词将出现在Settings > Language & input > Personal dictionary
中.
Added words will appear in Settings > Language & input > Personal dictionary
.
如果您要实现自己的软键盘,建议您通过创建输入方法.这些建议通常显示在Candidates View
中.默认情况下,InputMethodService#onCreateCandidatesView()
返回null.您应该重写此方法以返回建议栏的实现.
If you are implementing your own soft keyboard, I suggest you go through Creating an Input Method. The suggestions are usually shown in the Candidates View
. By default, InputMethodService#onCreateCandidatesView()
returns null. You should override this method to return your implementation of the suggestions bar.
这是一个实现候选人"视图的示例项目: SoftKeyboard .
Here's a sample project that implements the Candidates view: SoftKeyboard.
更多信息:
单词和短语建议进入候选人视图.有关如何创建&的信息在上面提到的示例项目中填充它.
Word and phrase suggestions go in the candidates view. Info about how to create & populate it are in the sample project mentioned above.
据我所知,选择要建议的单词/短语是开发人员的责任. Android不会为您提供这些功能.您可能需要一套词典-您计划支持的每种语言/地区都需要一套词典.您可能还需要维护一个用户指定单词的字典.
As far as I know, the selection of what words/phrases to suggest is developer's responsibility. Android does not provide those for you. You will probably need a set of dictionaries - one for each language/locale you plan on supporting. You may also want to maintain a dictionary of user-specified words.
Android的默认键盘使用以下这些:链接
Android's default keyboard uses these: Link
如果下载其中之一,则将其解压缩并使用文本编辑器打开:
If you download one of these, unpack it and open with a text editor:
dictionary=main:en,locale=en,description=English,date=1402373178,version=47
word=the,f=222,flags=,originalFreq=222
word=to,f=215,flags=,originalFreq=208
word=of,f=214,flags=,originalFreq=214
word=and,f=212,flags=,originalFreq=212
word=in,f=210,flags=,originalFreq=210
.... 165,635 more lines
很明显,频率在确定单词是否适合作为建议的过程中起着举足轻重的作用.当用户键入ta
时,您可能不想建议tachometer
.您可能愿意建议take
-频率可以帮助您.
As apparent, the frequency plays a pivotal role in determining the suitability of a word as a suggestion. You probably don't want to suggest tachometer
when the user types ta
. You probably do want to suggest take
- frequency helps you there.
自动更正:
word=id,f=99,flags=,originalFreq=99
shortcut=I'd,f=whitelist
标志表明适当性:
word=goddamn,f=0,flags=offensive,originalFreq=62
即使您决定使用这些词典,也必须由您来解析它们并获得有意义建议的代码.
Even if you decide to use these dictionaries, the code to parse them and obtain meaningful suggestions will have to come from you.
两篇文章(均由Peter Kankowski撰写)谈论predictive text input
& spelling correction
:
Two articles (both by Peter Kankowski) that talk about predictive text input
& spelling correction
:
候选人视图:
关于CandidatesView的第一件事:它是可选的.实际上,LatinIME
(Android的默认软键盘)不使用它.相反,LatinIME
有其自己的实现-SuggestionStripView
-与之类似. InputMethodService#onCreateCandidatesView()
的默认行为是返回null.如果选择提供自己的实现,请不要重写此方法.
The first thing you should know about the CandidatesView: it is optional. In fact, LatinIME
(android's default soft keyboard) does not use it. Instead LatinIME
has its own implementation - SuggestionStripView
- which is similar. The default behavior of InputMethodService#onCreateCandidatesView()
is to return null. If you choose to provide your own implementation, don't override this method.
您需要确定CandidatesView的外观.一种可能的实现方式可以是HorizontalScrollView
.在评估建议之后(例如,用户开始写"as",并且您的建议逻辑为您提供了一个List<String>
,其中包含"has","was","assist","ask","asked","asking" ",假设"),创建&将保存这些字符串的TextViews
添加到HorizontalScrollView(LinearLayout)
.这样,用户可以水平滚动并单击想要的单词.
You need to decide what your CandidatesView should look like. One possible implementation can be a HorizontalScrollView
. After you evaluate your suggestions (for example, user start writing "as", and your suggestion-logic gives you a List<String>
containing "has", "was", "assist", "ask", "asked", "asking", "assume"), create & add TextViews
holding these strings to the HorizontalScrollView(LinearLayout)
. This way, user can scroll horizontally and choose the intended word by clicking on it.
由您自己决定是使用API还是自行处理CandidatesView
.如果要使用API,请覆盖InputMetodService#onCreateCandidatesView()
,填充自定义布局,然后将其返回.保留对它的引用,以便您可以在需要时进行更新.要控制CandidatesView's
可见性,请使用方法setCandidatesViewShown(boolean)
.
It is up to you to decide whether to use the API or handle the CandidatesView
yourself. If you want to use the API, override InputMetodService#onCreateCandidatesView()
, inflate your custom layout, then return it. Hold a reference to it, so you can update it when required. To control CandidatesView's
visibility, use the method setCandidatesViewShown(boolean)
.
这篇关于如何在Android键盘中包含建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!