android – 在WebView中显示数字键盘

前端之家收集整理的这篇文章主要介绍了android – 在WebView中显示数字键盘前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个webview,我在登录屏幕上手动显示键盘并专注于输入字段.

领域:

<input type="password" maxlength="4" pattern="[0-9]*" id="pin">

聚焦和显示键盘

webView.requestFocus(View.FOCUS_DOWN);
webView.loadUrl("javascript:document.getElementById('pin').focus();");
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(webView,InputMethodManager.SHOW_IMPLICIT);

登录屏幕仅包含PIN样式代码字段,用户将在其中输入4位数字.问题是,默认情况下,它显示常规的alpha键盘,我希望它显示数字键盘.我已经看到很多使用EditText属性控制键盘类型的例子,例如

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
EditText.setInputType(InputType.TYPE_CLASS_PHONE); [select Input type as according to ur requirement]
mgr.showSoftInput(EditText,InputMethodManager.SHOW_IMPLICIT);

但是有可能告诉键盘本身在WebView上显示什么类型或设置属性

解决方法

这条路线对我有用.创建WebView的子类,并覆盖onCreateInputConnection方法.选择WebView中的输入字段时会调用方法,并且您可以自定义事件的处理方式.这是一个例子:
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    BaseInputConnection ic = new BaseInputConnection(this,true);
    outAttrs.inputType = InputType.TYPE_CLASS_NUMBER; // Tells the keyboard to show the number pad
    return ic;
}
原文链接:https://www.f2er.com/android/309711.html

猜你在找的Android相关文章