简体   繁体   English

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

[英]Android Custom Keyboard - How to get browsers InputType?

I'm trying to make a custom Android Keyboard (min API 16) with keyboard for every Input Type (like simple text, numeric, email, URL address, etc). 我正在尝试使用键盘为每种输入类型(如简单文本,数字,电子邮件,URL地址等)制作自定义Android键盘(最小API 16)。 The thing I don't get it is how to get the browsers URL TextEditor InputType so I can make a keyboard with 'Access' button. 我不明白的是如何获取浏览器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;
    }
}

But I still get as InputType the InputType.TYPE_CLASS_TEXT . 但我仍然将InputType作为InputType.TYPE_CLASS_TEXT

I think I've tried almost all the InputType but none works to determine when I'm typing in the URLs text box. 我想我已经尝试了几乎所有的InputType但没有一个可以确定我何时输入了URL文本框。 I need a solution to find the Input Type of URL TextEditor. 我需要一个解决方案来查找URL TextEditor的输入类型。

BTW: How do I perform an Access or Go action to browser, through a KeyEvent passed to getCurrentInputConnection().sendKeyEvent() ? 顺便说一句:如何通过传递给getCurrentInputConnection().sendKeyEvent()KeyEvent对浏览器执行AccessGo操作getCurrentInputConnection().sendKeyEvent()

Later Edit. 稍后编辑。 Solution: 解:

@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 said the answer for handling the "Access", "GO" action, this is for other editor actions: @ ray20表示处理“Access”,“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动作

getCurrentInputConnection().performEditorAction(EditorInfo.IME_ACTION_GO);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM