Tôi triển khai phương thức nhập được gọi là RemoteInput chỉ cần mở rộng InputMethodService, không có InputViews và không có bàn phím. Khi người dùng chọn RemoteInput làm IME mặc định, RemoteInput sẽ gửi trạng thái đầu vào hiện tại tới thiết bị khác và người dùng có thể thực hiện thao tác nhập từ xa (sử dụng giao thức khách hàng của chúng tôi). Khi nhập xong, văn bản được nhập vào thiết bị khác sẽ được gửi trở lại thiết bị hiện tại, sau đó RemoteInput cam kết văn bản vào thành phần giao diện người dùng hiện tại (chẳng hạn như EditText) sử dụng InputConnection.commitText (CharSequence text, int newCursorPosition)
.InputConnection.commitText (văn bản CharSequence, int newCursorPosition) chỉ có thể cam kết các ký tự và số bằng tiếng Anh?
Nó hoạt động hoàn hảo khi văn bản được nhập từ xa là ký tự tiếng Anh và số, nhưng khi nói đến các ký tự khác thì mọi thứ trở nên sai. Tôi đã tìm thấy rằng InputConnection.commitText
lọc các ký tự khác. Ví dụ: tôi nhập hello你好
, chỉ hello
được cam kết thành công. Và hơn thế nữa:
hello world
==>helloworld
hello,world!!
==>helloworld
Bất cứ điều gì bạn nói về vấn đề này sẽ rất hữu ích, cảm ơn trước.
Đây là mã của tôi:
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();
}
}