Codewords work hard! Please indicate the source!
0. Preface
I am working on a remote terminal recently, and I need to input text remotely, but because of network transmission, it often happens that the click becomes a long press, double click, etc...
So the product put forward a demand to make a remote input method. I thought about Android for ten years, and the mere input method should have already grabbed the data.
——Unexpectedly , I did not expect that the official Android input method documents were written sparsely , and the relevant information was copied from the official documents , which was useless at all! ! !
Angrily, I decided to write a real document by myself, huh!
1. Create a service
First create a class that inherits InputMethodService
public class FastInputIME extends InputMethodService{
}
Then declare the service
<service android:name="FastInputIME"
android:label="牛逼哄哄的输入法"
android:permission="android.permission.BIND_INPUT_METHOD">
<intent-filter>
<action android:name="android.view.InputMethod" />
</intent-filter>
<meta-data android:name="android.view.im"
android:resource="@xml/method" />
</service>
Finally, use the universal key (Alt+Enter) to create the missing @xml/method file and write the following
<?xml version="1.0" encoding="utf-8"?>
<input-method xmlns:android="http://schemas.android.com/apk/res/android"
android:settingsActivity="com.package.SettingsActivity"
android:supportsSwitchingToNextInputMethod="true">
</input-method>
At this point, you can already see the awesome input method in the settings:
2. Create a keyboard layout
To make it easy to read, write a very, very simple numeric keyboard. Note that the label name is Keyboard , not KeyboardView in the syntax prompt:
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Row>
<Key android:codes="1" android:keyLabel="1" />
<Key android:codes="2" android:keyLabel="2" />
<Key android:codes="3" android:keyLabel="3" />
<Key android:codes="4" android:keyLabel="4" />
<Key android:codes="5" android:keyLabel="5" />
<Key android:codes="6" android:keyLabel="6" />
<Key android:codes="7" android:keyLabel="7" />
<Key android:codes="8" android:keyLabel="8" />
<Key android:codes="9" android:keyLabel="9" />
<Key android:codes="0" android:keyLabel="0" />
</Row>
</Keyboard>
Among them, keyLabel is the text displayed on the button, and codes is the keycode to be used for monitoring in a while~
3. Create callbacks, bind keyboards, and add input events
Because it is very simple, you can just look at the part where I wrote the comment~
package com.eternity.lab;
import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
public class FastInputIME extends InputMethodService implements KeyboardView.OnKeyboardActionListener {
private InputConnection ic;
@Override
public View onCreateInputView() {
KeyboardView keyboardView = new KeyboardView(getApplicationContext(), null);
//在此处导入键盘布局
Keyboard keyboard = new Keyboard(getApplicationContext(), R.layout.keyboard);
keyboardView.setKeyboard(keyboard);
keyboardView.setOnKeyboardActionListener(this);
return keyboardView;
}
@Override
public void onWindowShown() {
super.onWindowShown();
/** 进入此回调时,用户将开始输入
在此时保存InputConnection,用于后续的输入操作 **/
ic = getCurrentInputConnection();
}
@Override
public void onWindowHidden() {
super.onWindowHidden();
/** 进入此回调时,用户已结束输入
在此时清理InputConnection **/
ic = null;
}
@Override
public void onPress(int primaryCode) {
/** 此处的primaryCode,即键盘布局中的code属性,可以根据code来commit需要输入的文字
第二个参数是输入的位置,注意是从右往左,0代表向右边插入一个字符 **/
ic.commitText("" + primaryCode, 0);
}
@Override
public void onRelease(int primaryCode) {
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
}
@Override
public void onText(CharSequence text) {
}
@Override
public void swipeLeft() {
}
@Override
public void swipeRight() {
}
@Override
public void swipeDown() {
}
@Override
public void swipeUp() {
}
}
At this point, a simple custom input method is complete~
The remaining complex functions can be realized one by one in the documentation:
https://developer.android.google.cn/guide/topics/text/creating-input-method
Finally, it is customary to ask for money: