【问题标题】:Check if Spell Checker is on?检查拼写检查器是否打开?
【发布时间】:2015-02-14 09:48:49
【问题描述】:

我在我的 Android 应用程序中使用拼写检查器。如果在语言和输入设置中关闭,则应用程序崩溃。它需要打开才能使应用程序正常工作。

有没有一种方法可以在使用它之前进行检查以验证拼写检查器是否已打开或直接从代码中打开它?

【问题讨论】:

  • 您要禁用拼写更正吗?如果否,则发布错误日志
  • 不,我想检查它是打开还是关闭。还有有没有办法打开它。
  • If it is off in the Language and Input settings then the App crashes. 显示崩溃日志
  • 异常调度输入事件。 MessageQueue 回调中的异常:handleReceiveCallback E/MessageQueue-JNI(6238): java.lang.NullPointerException: 尝试调用虚拟方法“void android.view.textservice.SpellCheckerSession.getSuggestions(android.view.textservice.TextInfo, int)”空对象引用
  • 在调用 getSuggestions 之前只需检查其是否为空

标签: android spell-checking


【解决方案1】:

您可以简单地检查nullSpellCheckerSession 实例,并据此决定Spellchecker 是否打开:

代码片段:

final TextServicesManager tsm = (TextServicesManager) getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);

scs = tsm.newSpellCheckerSession(null, null, this, true);

if (scs != null) {
    scs.getSuggestions(new TextInfo(text.getText().toString()), 3);
} else {
    // Show the message to user
    Toast.makeText(this, "Please turn on the spell checker from setting", Toast.LENGTH_LONG).show();
    // You can even open the settings page for user to turn it ON
    ComponentName componentToLaunch = new ComponentName("com.android.settings",
                "com.android.settings.Settings$SpellCheckersSettingsActivity");
    Intent intent = new Intent();
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setComponent(componentToLaunch);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {
        this.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        // Error
    }
}

希望对你有所帮助ツ

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 2012-12-05
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多