Implementuję metodę wejściową o nazwie RemoteInput po prostu rozszerzam InputMethodService, bez InputViews i bez klawiatury. Gdy użytkownik wybierze domyślny IME, RemoteInput, RemoteInput wyśle bieżący stan wejścia do innego urządzenia, a użytkownik może zdalnie wykonać akcję wejściową (korzystając z naszego klienta). Po zakończeniu wprowadzania tekst wprowadzony w innym urządzeniu zostanie odesłany z powrotem do bieżącego urządzenia, a następnie RemoteInput zatwierdza tekst do bieżącego składnika interfejsu użytkownika (takiego jak EditText) przy użyciu InputConnection.commitText (CharSequence text, int newCursorPosition)
.InputConnection.commitText (tekst CharSequence, int newCursorPosition) może wyświetlać tylko angielskie znaki i liczby?
Działa idealnie, gdy zdalnie wprowadzanym tekstem są angielskie znaki i cyfry, ale jeśli chodzi o inne postacie, wszystko idzie nie tak. Zauważyłem, że InputConnection.commitText
filtruje inne znaki. Na przykład wpisałem hello你好
, tylko hello
został pomyślnie zatwierdzony. I więcej:
hello world
==>helloworld
hello,world!!
==>helloworld
coś powiedzieć na ten temat będzie pomocny, z góry dzięki.
Oto mój kod:
public class RemoteInput extends InputMethodService {
protected static String TAG = "RemoteInput";
public static final String ACTION_INPUT_REQUEST = "com.aidufei.remoteInput.inputRequest";
public static final String ACTION_INPUT_DONE = "com.aidufei.remoteInput.inputDone";
private BroadcastReceiver mInputReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (ACTION_INPUT_DONE.equals(intent.getAction())) {
String text = intent.getStringExtra("text");
Log.d(TAG, "broadcast ACTION_INPUT_DONE, input text: " + text);
input(text);
}
}
};
@Override
public void onCreate() {
super.onCreate();
registerReceiver(mInputReceiver, new IntentFilter(ACTION_INPUT_DONE));
}
@Override
public View onCreateInputView() {
//return getLayoutInflater().inflate(R.layout.input, null);
return null;
}
@Override
public boolean onShowInputRequested(int flags, boolean configChange) {
if (InputMethod.SHOW_EXPLICIT == flags) {
Intent intent = new Intent(ACTION_INPUT_REQUEST);
getCurrentInputConnection().performContextMenuAction(android.R.id.selectAll);
CharSequence text = getCurrentInputConnection().getSelectedText(0);
intent.putExtra("text", text==null ? "" : text.toString());
sendBroadcast(intent);
}
return false;
}
public void input(String text) {
InputConnection inputConn = getCurrentInputConnection();
if (text != null) {
inputConn.deleteSurroundingText(100, 100);
inputConn.commitText(text, text.length());
}
//inputConn.performEditorAction(EditorInfo.IME_ACTION_DONE);
}
@Override
public void onDestroy() {
unregisterReceiver(mInputReceiver);
super.onDestroy();
}
}