我正在尝试为每种输入类型(如简单文本、数字、电子邮件、URL 地址等)制作带有键盘的自定义 Android 键盘(最小 API 16)。我不明白的是如何获取浏览器 URL TextEditor InputType,以便我可以制作带有“访问”按钮的键盘。
@Override public void onStartInput(EditorInfo attribute, boolean restarting) {
super.onStartInput(attribute, restarting);
switch (attribute.inputType & InputType.TYPE_MASK_CLASS) {
case InputType.TYPE_CLASS_NUMBER:
case InputType.TYPE_CLASS_DATETIME:
case InputType.TYPE_CLASS_PHONE:
mCurKeyboard = symKeyboard_1;
break;
case InputType.TYPE_TEXT_VARIATION_URI:
case InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT:
case InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS:
case InputType.TYPE_TEXT_VARIATION_EMAIL_SUBJECT:
case InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS:
case InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS:
mCurKeyboard = webKeyboard;
Log.d("debug", "web keyboard");
break;
case InputType.TYPE_CLASS_TEXT:
mCurKeyboard = myKeyboard;
Log.d("debug", "text");
break;
default:
mCurKeyboard = myKeyboard;
Log.d("debug", "default");
break;
}
}
但我仍然将 InputType.TYPE_CLASS_TEXT
作为 InputType。
我想我已经尝试了几乎所有的 InputType
,但没有一个能确定我何时在 URL 文本框中输入。我需要一个解决方案来找到 URL TextEditor 的输入类型。
顺便说一句:如何通过传递给 getCurrentInputConnection() 的
?KeyEvent
对浏览器执行 Access
或 Go
操作。发送键事件()
稍后编辑。解决方案:
@Override public void onStartInput(EditorInfo attribute, boolean restarting) {
super.onStartInput(attribute, restarting);
switch (attribute.inputType & InputType.TYPE_MASK_CLASS) {
case InputType.TYPE_CLASS_NUMBER:
case InputType.TYPE_CLASS_DATETIME:
case InputType.TYPE_CLASS_PHONE:
currentKeyboard = numericKeyboard;
break;
case InputType.TYPE_CLASS_TEXT:
int webInputType = attribute.inputType & InputType.TYPE_MASK_VARIATION;
if (webInputType == InputType.TYPE_TEXT_VARIATION_URI ||
webInputType == InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT ||
webInputType == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
|| webInputType == InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS) {
currentKeyboard = webKeyboard;
}else{
currentKeyboard = latinKeyboard;
}
break;
default:
currentKeyboard = latinKeyboard;
break;
}
}
@ray20 说了处理“访问”、“GO”操作的答案,这是针对其他编辑器操作的:
private void handleAction(){
EditorInfo curEditor = getCurrentInputEditorInfo();
switch (curEditor.imeOptions & EditorInfo.IME_MASK_ACTION){
case EditorInfo.IME_ACTION_DONE:
getCurrentInputConnection().performEditorAction(EditorInfo.IME_ACTION_DONE);
break;
case EditorInfo.IME_ACTION_GO:
getCurrentInputConnection().performEditorAction(EditorInfo.IME_ACTION_GO);
break;
case EditorInfo.IME_ACTION_NEXT:
getCurrentInputConnection().performEditorAction(EditorInfo.IME_ACTION_NEXT);
break;
case EditorInfo.IME_ACTION_SEARCH:
getCurrentInputConnection().performEditorAction(EditorInfo.IME_ACTION_SEARCH);
break;
case EditorInfo.IME_ACTION_SEND:
getCurrentInputConnection().performEditorAction(EditorInfo.IME_ACTION_SEND);
break;
default:
handleClose(); //method that hides the keyboard with no action performed
break;
}
}
最佳答案
只执行GO Action
getCurrentInputConnection().performEditorAction(EditorInfo.IME_ACTION_GO);
关于Android 自定义键盘 - 如何获取浏览器 InputType?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31159295/