Android自定义键盘 - 如何检测请求的键盘类型
问题描述:
正在关注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;
}
答
您可以在您的自定义键盘类中覆盖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:
// ...
}
// ...
}