Android自定义键盘 - 如何检测所请求的键盘类型

时间:2017-04-14 15:28:33

标签: android android-softkeyboard

关注this tutorial我创建了一个功能齐全的Android OS键盘。它是一个标准的qwerty字母/数字。

我有一个数字键盘的第二个键盘标记。

我无法检测到的是文本输入框指定的键盘类型。 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 个答案:

答案 0 :(得分: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:
            // ...
    }

    // ...
}