我有一个简单的活动,用于SpellCheckerSession
建议单词的相似匹配。但如果键盘显示拼写检查器不起作用。也许是因为键盘也在使用拼写检查器。
我的简化活动是:
public class TestActivity extends AppCompatActivity implements SpellCheckerSession.SpellCheckerSessionListener{
private SpellCheckerSession spellChecker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
test();
}
void test() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
spellChecker.getSentenceSuggestions(new TextInfo[] {new TextInfo("bool")}, 4);
test();
}
}, 2000);
}
@Override
public void onGetSuggestions(SuggestionsInfo[] results) {}
@Override
public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {
Log.i("tag", "results size: " + results.length);
for (SentenceSuggestionsInfo result : results) {
final int len = result.getSuggestionsCount();
for (int j = 0; j < len; ++j) {
int a = result.getSuggestionsInfoAt(j).getSuggestionsCount();
Log.i("tag", "getSuggestionsCount : " + a);
for (int i = 0; i < a; ++i) {
Log.i("tag", "Suggestion: " + result.getSuggestionsInfoAt(j).getSuggestionAt(i));
}
}
}
}
@Override
public void onResume() {
super.onResume();
final TextServicesManager tsm = (TextServicesManager)
getApplicationContext().getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
if (tsm != null) {
spellChecker = tsm.newSpellCheckerSession(null, Locale.US, this, true);
}
}
@Override
protected void onPause() {
super.onPause();
if (spellChecker != null) {
spellChecker.close();
}
}
}
在这个活动中,我getSentenceSuggestions
每 2 秒调用一次以测试它是否工作。在我的活动布局中,我有一个 EditText。午餐后应用程序 spellChecker 工作并在 logcat 中显示建议,但如果单击 editText 以显示 keyboaar,则 spellChecker 不起作用并getSuggestionsCount
在 logcat 中显示 -1。如果隐藏键盘一切正常。