2017-04-14 49 views
1

正在關注this tutorial我已經創建了一個完全可用的Android OS鍵盤。它是一個標準的QWERTY字母/數字。Android自定義鍵盤 - 如何檢測請求的鍵盤類型

我有數字鍵盤的第二個鍵盤標記。

似乎我無法檢測是正在由文本輸入框中指定什麼類型的鍵盤。 edittext指定的類型爲 editText.setInputType(InputType.TYPE_CLASS_TEXT); 但我的IME服務如何檢測到這一點,以便它可以呈現正確的鍵盤?

public class MyKeybdIME extends InputMethodService implements KeyboardView.OnKeyboardActionListener { 
    private KeyboardView kv; 
    private Keyboard keyboard; 
    private Keyboard numboard; 
    private boolean caps = false; 
    @Override 
    public View onCreateInputView() { 
     kv = (MKeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null); 
     keyboard = new Keyboard(this, R.xml.qwertyfull); 
     numboard = new Keyboard(this, R.xml.num); 

//  InputMethodManager imm = (InputMethodManager)  getSystemService(Context.INPUT_METHOD_SERVICE); 
//How can you detect what is being asked for?  
//  imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); 
// Or am I on the wrong path for this part? 


     kv.setKeyboard(keyboard);//... Or numboard when the entry requests a numeric keyboard 
     kv.setOnKeyboardActionListener(this); 
     return kv; 
    } 

回答

1

您可以在您的自定義鍵盤類中覆蓋onStartInput。以下是取自sample Android keyboard的相關代碼:

@Override public void onStartInput(EditorInfo attribute, boolean restarting) { 
    // ... 

    switch (attribute.inputType & InputType.TYPE_MASK_CLASS) { 
     case InputType.TYPE_CLASS_NUMBER: 
      // ... 
      break; 
     case InputType.TYPE_CLASS_DATETIME: 
      // ... 
      break; 
     case InputType.TYPE_CLASS_PHONE: 
      // ... 
      break; 
     case InputType.TYPE_CLASS_TEXT: 
      // ... 
      break; 
     default: 
      // ... 
    } 

    // ... 
}