Recently, I encountered a problem in Android ndk development: I used OpenGL to draw the input interface at the underlying layer, but I couldn't call the input method (IME). I could only call it up. I used JNI to notify the Java layer to call the input method.
The requirement for the Java layer is that only the input method is popped up, and the key events and input content of the keyboard of the input method are directly transmitted to the underlying layer. The seemingly simple requirement turned me around for a few days.
Here we will summarize the problems and solutions I have encountered.
1. Call the Input Method
InputMethodManager input = (InputMethodManager)mApp.getSystemService(Context.INPUT_METHOD_SERVICE);input.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS);
Input. togglesoftinput (): If the input method has been enabled, hide it. If it is hidden, display it. You can also directly call the show () or hide () method of input to directly display or hide the data.
Note: The test results show that the system input method is always enabled and can only be hidden.
2. A view or edittext object must be provided for the input method to receive the input content.
Call input in view or edittext. togglesoftinput () method, or input. showsoftinput (view, flags) uses view or edittext as parameters to enable the soft keyboard. The input content is transmitted to the specified object.
Edittext can be directly obtained through gettext;
View needs to implement the following methods
public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new MyInputConnection(this, false); }
Create a class that inherits baseinputconnection
class MyInputConnection extends BaseInputConnection{ public MyInputConnection(View targetView, boolean fullEditor) { super(targetView, fullEditor); } public boolean commitText(CharSequence text, int newCursorPosition) { Log.i("ime", "input text:" + text); return true; } public boolean sendKeyEvent(KeyEvent event) { Log.i("ime", "sendKeyEvent()" + event.getKeyCode()); return true; } }
The inputconnection is used to establish an input connection. Once an input content input method is used, commtext is called to pass the content in;
Another important method is sendkeyevent (). Click Enter and backspace on the keyboard. The event is not passed to commtext, but to sendkeyevent (). You can use this method to process the event as needed.
3. Soft Keyboard style and attribute settings
There are many methods in the edittext API that can be called directly to set the attributes of the software disk. For details, see the android API documentation. View needs to be set in the oncreateinputconnection (editorinfo outattrs) method through the outattrs attribute.
public InputConnection onCreateInputConnection(EditorInfo outAttrs) { outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI;// outAttrs.inputType = EditorInfo.TYPE_TEXT_VARIATION_PASSWORD;// outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE; return new MyInputConnection(this, false); }
The most important style of a software disk is full screen mode and non-full screen mode. In the case of a portrait screen, there is no full screen mode, and in the case of a horizontal screen (especially Game Development) both modes are used more.
It is worth noting that in the case of landscape screen, the input method is called through view. If it is not in full screen mode, you need to set outattrs. imeoptions = editorinfo. ime_flag_no_extract_ui. Otherwise, the soft keyboard does not have a candidate bar, and Chinese characters cannot be entered. The specific reason is unclear. If you have any knowledge, please advise.
/* Todo continuous update */