2013-03-14 23 views
6

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(); 
    } 
} 

Trả lời

-2

Nếu bạn đang sử dụng một ngôn ngữ khác ngoài tiếng Anh bạn được yêu cầu phải sử dụng Unicode. Bạn cần đính kèm phông chữ Unicode vào dự án của mình và đặt kiểu chữ của phần tử (ví dụ: EditText) thành phông chữ bạn đã đính kèm. Xem phần sau để tìm hiểu cách thêm phông chữ tùy chỉnh vào ứng dụng của bạn.

http://tharindudassanayake.wordpress.com/2012/02/25/use-sinhala-fonts-for-your-android-app/

Lựa chọn thứ hai sẽ được nhổ tận gốc điện thoại và cài đặt các font cần thiết.

Các vấn đề liên quan