开发者

How to disable displaying "suggestions" on the Soft Keyboard

I want to turn off displaying "Suggested Words" on the soft/virtual keyboard when someone is using my application (only on certain Activities). For the default Android keyboard, this can be found under 'Settings' (under Word Suggestion Settings).

Is there a way to disable it only within your applic开发者_StackOverflow中文版ation, without requiring the user to manually go and do it? I basically want the user to type words without providing any hints.

Thanks!


When developing for 2.0+, the supposed way is setting android:inputType="textNoSuggestions" (ref). Unfortunately, suggestions are still shown on HTC Desire 2.2 (and probably other HTC Sense devices as well).
Using android:inputType="textVisiblePassword"will not help as well as the software keyboard by HTC won't allow you to switch languages.
So I stick to android:inputType="textFilter" to disable suggestions.


You can disable suggestions on the Soft Keyboard by adding the following line in the xml -

android:inputType="textNoSuggestions"

However according to this article, it may or may not be supported by the IME (the keyboard).

If this issue occurs, the below method works for sure -

android:inputType="textNoSuggestions|textVisiblePassword"


This works for me with the stock keyboard, even on HTC with 2.2

final EditText et = (EditText) findViewById(R.id.SearchText);
et.setInputType(et.getInputType()
    | EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS
    | EditorInfo.TYPE_TEXT_VARIATION_FILTER);


On my 6P running Nougat, nothing worked. While it is empty the suggestion bar stays up because it has the microphone icon on the right side. In order to get rid of that, I used fingerup's suggestion in one of the comments and it worked! So I decided to write an actual answer so people don't miss it. To recap here's what I've used:

    android:inputType="textNoSuggestions|textFilter|textVisiblePassword"
    android:privateImeOptions="nm"

inputType="textNoSuggestions|textFilter|textVisiblePassword" prevents suggestions, and privateImeOptions="nm" (stands for "no microphone") prevents the bar from showing up because it still would since the mic button is in it. So it is necessary to use both attributes because if all you do is specify no mic then the bar still shows up with recommendations.
Thx again to fingerup for mentioning the nm trick. ;)


There are two ways that I know of to disable the auto-complete. One way is through the XML by setting the android:inputType="textVisiblePassword" in the layout xml.

The other way is through code such as the following

EdtiText editTextBox = findViewById(R.id.myEditTextView);
editTextBox.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);


I was facing the same issue on Samsung devices using:

android:inputType="textNoSuggestions|textFilter|textVisiblePassword"

only after setting the inputType to password and visiblePassword worked for me:

android:inputType="textPassword|textVisiblePassword"


android:inputType="textPhonetic" hides soft keyboard suggestions on Android 1.6.


hope this works for you,

editText.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_CAP_SENTENCES|InputType.TYPE_TEXT_FLAG_MULTI_LINE|InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);


If you are using InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS in activity, it will work fine. In case of DialogFragment, you should place InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS at onActivityCreated() handler as below :-

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EditText editTextUsername =  (EditText)dialogView.findViewById(R.id.txtUsername);
        editTextUsername.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜