【问题标题】:Input special characters in android WebView在android WebView中输入特殊字符
【发布时间】:2025-11-26 03:45:02
【问题描述】:

我想在 Android 网页视图中输入特殊字符(例如“ä”、“é”、“£”等)。 这些字符不包含在虚拟 KCM(关键字符映射)中。所以我无法检索与角色关联的键码并创建 KeyEvent。

我尝试了不同的方法:

String t = "testàaâäù";
char[] charArray = t.toCharArray();
if (charArray.length > 0) {
    KeyCharacterMap keyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
    KeyEvent[] events = keyCharacterMap.getEvents(charArray);
}
// events is null, because some characters aren't include in KCM
// it never works in any case

使用检测方法:

instrumentation.sendStringSync("a");                   // Display 'a'
instrumentation.sendStringSync("àâä");                 // Display nothing
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_H); // Display 'h'
// Instrumentation seems to use KCM for data entry

使用 ACTION_MULTIPLE 键事件:

KeyEvent event = new KeyEvent(SystemClock.uptimeMillis(), String.valueOf("aàâä"), 0, 0);
focusedView.dispatchKeyEvent(event);
// Doesn't work with WebView, but works with other components such as EditText.
// I get this error: Unimplemented WebView method onKeyMultiple called from: android.webkit.WebView.onKeyMultiple

我可以实现 onKeyMultiple (WebView) 方法来处理数据输入吗?有没有在 webview 中输入特殊字符的解决方案?我需要一个不需要 root 或创建键盘布局(用户不必选择键盘)的解决方案。

【问题讨论】:

    标签: android input webview special-characters keyevent


    【解决方案1】:

    在 WebView 内的输入字段上使用虚拟键盘书写时,我遇到了同样的问题。它不会识别像“á”这样的特殊字符。我做了什么:

    if (event.getAction() == KeyEvent.ACTION_MULTIPLE) {
        loadUrl("javascript:var prevText = document.activeElement.value;" +
                "var afterText = document.activeElement.value = prevText + \"" + event.getCharacters() + "\";");
        return false;
    }
    

    当操作是 Javascript 的 ACTION_MULTIPLE 时,我得到输入字段并附加特殊字符。

    这是全班:

    public class CustomWebView extends WebView {
    
        public CustomWebView (Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
            ExtenderInputConnection connection = new ExtenderInputConnection(this, false);
            outAttrs.imeOptions = EditorInfo.IME_ACTION_SEARCH;
            return  connection;
        }
    
        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
    
            if (event.getAction() == KeyEvent.ACTION_MULTIPLE) {
                loadUrl("javascript:var prevText = document.activeElement.value;" +
                        "var afterText = document.activeElement.value = prevText + \"" + event.getCharacters() + "\";");
                return false;
            }
    
            return super.dispatchKeyEvent(event);
        }
    
        public class ExtenderInputConnection extends BaseInputConnection implements InputConnection {
    
            public ExtenderInputConnection(View targetView, boolean fullEditor) {
                super(targetView, fullEditor);
            }
    
    
            @Override
            public boolean deleteSurroundingText(int beforeLength, int afterLength) {
    
                if (beforeLength == 1 && afterLength == 0) {
                    // backspace
                    return super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
                }
                return super.deleteSurroundingText(beforeLength, afterLength);
            }
        }
    }
    

    希望对你有帮助。

    【讨论】: