简体   繁体   English

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

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

I need the Spell Checker to be turned off using Android code. 我需要使用Android代码关闭拼写检查器。 How do I do that? 我怎么做? If it cannot be turned off by code, is there a way to display the spell checker options to the user so the user can turn it off manually? 如果无法通过代码关闭它,是否有办法向用户显示拼写检查选项,以便用户可以手动关闭它? thanks 谢谢

Add this line into your EditText : 将此行添加到EditText

android:inputType="textFilter"

And if your EditText accepts multiple lines, then do this: 如果您的EditText接受多行,那么执行以下操作:

android:inputType="textFilter|textMultiLine"

Update: 更新:

That is not possible to switch it off/on till now via code. 到目前为止,无法通过代码将其关闭/打开。 But you can do one thing, you can ask user to disable it and if user choose yes then open Language Settings Screen by following code: 但是你可以做一件事,你可以要求用户禁用它,如果用户选择是,则按照以下代码打开语言设置屏幕:

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

If this is for an EditText (I think that is what you want) here is a trick: 如果这是一个EditText(我认为这就是你想要的)这里有一个技巧:

android:inputType="textVisiblePassword"

You can also OR more types in, incase you wanted like "textCapSentences" as well or "textMultiLine" 您也可以使用更多类型,也可以选择“textCapSentences”或“textMultiLine”

eg android:inputType="textVisiblePassword|textMultiLine" 例如android:inputType =“textVisiblePassword | textMultiLine”

EDIT: You asked for doing this to allow for user to change this. 编辑:您要求这样做以允许用户更改此设置。 Well add some sort of settings, or a button, that enables / disables auto correct. 好吧添加某种设置或按钮,启用/禁用自动更正。 Then set the flag to true/false based on the flag saved in SharedPreferences. 然后根据SharedPreferences中保存的标志将标志设置为true / false。 You then retrieve the value any time you are creating the activity that contains the EditText. 然后,只要创建包含EditText的活动,就可以检索该值。

ie When user enables auto correct (some button or switch) 即当用户启用自动更正(某些按钮或开关)时

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

When you want to check if the pref is enabled: 如果要检查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);
}

You basically locally store the user's setting for allowing for auto correct and change the input types of your EditTexts accordingly based on the setting saved. 您基本上在本地存储用户的设置以允许自动更正,并根据保存的设置相应地更改EditTexts的输入类型。 If it is enabled, no user input will cause the keyboard to include auto correct / spell checker. 如果启用,则没有用户输入将导致键盘包含自动更正/拼写检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM