在InputMethodService的onCreateInputView中,我有一个BroadcastReceiver,它侦听来自应用程序的事件。我的BroadcastReceiver收到了文本,然后尝试插入到当前文本字段中。问题是从未插入当前文本字段的文本一切正常,除非未在editText字段中插入文本。
我在这里发布我的代码片段
@Override
public View onCreateInputView() {
kv = (KeyboardView) getLayoutInflater().inflate(R.layout.activity_main, null);
keyboard = new Keyboard(this, R.xml.qwerty);
kv.setKeyboard(keyboard);
kv.setOnKeyboardActionListener(this);
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String resultData = intent.getStringExtra(Main2Activity.SERVICE_MESSAGE);
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.commitText(resultData, resultData.length());
}
}
};
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
lbm.registerReceiver(broadcastReceiver, new IntentFilter(Main2Activity.SERVICE_RESULT));
return kv;
}