Android 自定义键盘 - 如何获取浏览器 InputType?

标签 android android-softkeyboard

我正在尝试为每种输入类型(如简单文本、数字、电子邮件、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 对浏览器执行 AccessGo 操作。发送键事件() ?

稍后编辑。解决方案:

@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/

相关文章:

c# - 调用 Google GCM 时未经授权

android - 阻止特定运营商的 list 代码?

android - Qwerty 键盘在我的 Android 项目中不起作用

android - 如何在 Android 上为 EditText 设置 MultiLine 和 imeOptions ="actionDone"?

android - 当按下键盘上的 GO 按钮时,Android 中的默认提交按钮是哪个?

android - 如何让Android应用程序(电视盒)识别或访问USB网络摄像头?

android - 带有Git的Android Studio Gradle外部目录/库

android - 如何在 Android 中调用默认文件选择器?

android - 如何在 xml 键盘 View 上创建自定义属性?

android - 如何在键盘事件上打开选择输入模式