如何使用 Android 中的代码关闭拼写检查器?

How do I turn off Spell checker using code in Android?

我需要使用 Android 代码关闭拼写检查器。我怎么做?如果无法通过代码将其关闭,是否可以向用户显示拼写检查器选项,以便用户手动将其关闭?谢谢

如果这是用于 EditText(我认为这就是您想要的),这里有一个技巧:

android:inputType="textVisiblePassword"

您也可以或输入更多类型,以防您也想要 "textCapSentences" 或 "textMultiLine"

例如 android:inputType="textVisiblePassword|textMultiLine"

编辑:您要求这样做是为了允许用户更改它。好吧,添加某种设置或按钮,启用/禁用自动更正。然后根据SharedPreferences中保存的flag设置flag为true/false。然后,您可以在创建包含 EditText 的 activity 时随时检索该值。

即当用户启用自动更正时(某些按钮或开关)

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putBoolean("autoCorrect", true).apply();

当你想检查pref是否启用时:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean autoCorrectEnabled = prefs.getBoolean("autoCorrect", false);

//change the settings of EditTexts to turn off auto correct / spell checker
if(!autoCorrectEnabled){
    input.setInputType(TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

//enable spell checker 
}else{
    input.setInputType(TYPE_CLASS_TEXT);
}

您基本上是在本地存储用户设置以允许自动更正,并根据保存的设置相应地更改 EditText 的输入类型。如果启用,则没有用户输入会导致键盘包含自动更正/拼写检查器。

将此行添加到您的 EditText:

android:inputType="textFilter"

如果您的 EditText 接受多行,则执行此操作:

android:inputType="textFilter|textMultiLine"

更新:

到目前为止,无法通过代码切换它 off/on。但是你可以做一件事,你可以要求用户禁用它,如果用户选择是,然后通过以下代码打开语言设置屏幕:

Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setClassName("com.android.settings", "com.android.settings.LanguageSettings");
        startActivity(intent);