如何在 Android 键盘中包含建议 [英] How to include suggestions in Android Keyboard

查看:22
本文介绍了如何在 Android 键盘中包含建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Android SoftKeyboard.我已经为键盘创建了布局,但不知道如何包含在我们在 EditText 中输入一些单词时出现的建议.
例如,如果我写Kn",则建议"中会显示Known"和Known".
所以我的问题是 -
1) 如何在 Android 软键盘中包含建议?
2) 有什么办法可以包含我们自己的建议列表吗?
提前感谢很多.
我已经检查了

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {//在 JellyBean & 上上面,您可以提供快捷方式和明确的语言环境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);}

您需要将此权限添加到您的清单中:

<uses-permission android:name="android.permission.WRITE_USER_DICTIONARY"/>

添加的词会出现在设置>语言&输入 >个人词典.

如果您正在实现自己的软键盘,我建议您通过 Creating an Input方法.建议通常显示在 Candidates View 中.默认情况下,InputMethodService#onCreateCandidatesView() 返回 null.您应该覆盖此方法以返回您对建议栏的实现.

以下是实现 Candidates 视图的示例项目:SoftKeyboard.

更多信息:

单词和短语建议进入候选视图.关于如何创建 & 的信息填充它在上面提到的示例项目中.

据我所知,选择建议的单词/短语是开发人员的责任.Android 不会为您提供这些.您可能需要一套词典 - 一个用于您计划支持的每种语言/区域设置.您可能还想维护用户指定单词的字典.

Android 的默认键盘使用这些:链接

如果您下载其中之一,请解压并使用文本编辑器打开:

dictionary=main:en,locale=en,description=English,date=1402373178,version=47word=the,f=222,flags=,originalFreq=222word=to,f=215,flags=,originalFreq=208word=of,f=214,flags=,originalFreq=214word=and,f=212,flags=,originalFreq=212word=in,f=210,flags=,originalFreq=210.... 165,635 行

很明显,频率在决定一个词是否适合作为建议方面起着举足轻重的作用.当用户键入 ta 时,您可能不想建议 tachometer.您可能确实想要建议 take - 频率可以帮助您.

自动更正:

word=id,f=99,flags=,originalFreq=99快捷方式=我,f=白名单

标志表明适当性:

word=goddamn,f=0,flags=offensive,originalFreq=62

即使您决定使用这些词典,解析它们并获得有意义的建议的代码也必须来自您.

两篇文章(均由 Peter Kankowski 撰写)讨论了预测文本输入 &拼写更正:

使用 DAWG 进行预测文本输入

使用三元 DAG 进行拼写校正

候选人视图:

关于 CandidatesView 您应该知道的第一件事:它是可选的.事实上,LatinIME(android 的默认软键盘)并没有使用它.相反,LatinIME 有它自己的实现 - SuggestionStripView - 这是类似的.InputMethodService#onCreateCandidatesView() 的默认行为是返回 null.如果您选择提供自己的实现,请不要覆盖此方法.

您需要决定您的 CandidatesView 应该是什么样子.一种可能的实现可以是 Horizo​​ntalScrollView.在你评估你的建议之后(例如,用户开始写as",你的建议逻辑给你一个 List 包含has"、was"、assist"、ask"", "asked", "asking", "assume"), create &将保存这些字符串的 TextViews 添加到 Horizo​​ntalScrollView(LinearLayout).这样,用户可以水平滚动并通过点击来选择想要的词.

由您决定是使用 API 还是自己处理 CandidatesView.如果您想使用该 API,请覆盖 InputMetodService#onCreateCandidatesView(),扩充您的自定义布局,然后返回它.保留对它的引用,以便您可以在需要时更新它.要控制 CandidatesView 的 可见性,请使用方法 setCandidatesViewShown(boolean).

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.

解决方案

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"/>

Added words will appear in Settings > Language & input > Personal dictionary.

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.

Here's a sample project that implements the Candidates view: SoftKeyboard.

More info:

Word and phrase suggestions go in the candidates view. Info about how to create & populate it are in the sample project mentioned above.

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'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

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.

Autocorrection:

word=id,f=99,flags=,originalFreq=99
shortcut=I'd,f=whitelist

The flags indicate appropriateness:

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.

Two articles (both by Peter Kankowski) that talk about predictive text input & spelling correction:

Using DAWG for predictive text input

Using Ternary DAGs for Spelling Correction

CandidatesView:

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.

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.

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屋!

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