简体   繁体   中英

How to programmatically disable predictive text (with no EditText)

There is an Android application containing self-made input field (no TextView or EditText elements), so I have to show/hide keyboard, handle user input and show entered symbols on my own.

I need to disable predictive text mode for standard view. Unfortunately Android View class (android.view.View) has no function setInputType .

There is a probable solution. Get InputConnection of given view and change its properties. But I cannot find how to get and set instance of current InputConnection , unfortunately function onCreateInputConnection is not called either.

Is there any method to disable predictive text mode for standard view?

Something I've used is below- specifically the tag "textNoSuggestions" I think would work for you!

<EditText android:layout_marginLeft="10px" 
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:layout_marginRight="10px"
android:id="@+id/setupactivity_ftpsite" 
android:inputType="textNoSuggestions|textUri">

Sorry here is the answer in a more concise manner!

Something similar to this:

1) show the keyboard:

InputMethodManager imm =  (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(myCustomView, InputMethodManager.SHOW_IMPLICIT);

2) in myCustomView (extends view), add:

 InputConnection onCreateInputConnection (EditorInfo outAttrs) {
     InputConnection ic = new EditableInputConnection(this);
     outAttrs.inputType = TYPE_TEXT_FLAG_NO_SUGGESTIONS;
     outAttrs.initialCapsMode = ic.getCursorCapsMode(outAttrs.inputType); //guess on this
     return ic;
}

This is the overall gist of what should be done. You may want to OR outAttrs.inputType instead of set equal so it preserves the default state, or call the parent onCreateInputConnection first and then just set your outAttrs.inputType (not sure if this will work or not). This should hopefully get you pretty close to your solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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