在web视图显示数字键盘 [英] Show numeric keyboard in a WebView
问题描述
我有一个web视图我在哪里手动显示登录屏幕上的键盘,注重输入字段。
I have a webview where I am manually showing the keyboard on the login screen and focusing on the input field.
字段:
<input type="password" maxlength="4" pattern="[0-9]*" id="pin">
焦点,并显示键盘:
Focus and show keyboard:
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风格code字段,用户将进入4位数字。现在的问题是,默认情况下它显示了常规字母键盘,我想让它显示数字键盘代替。我见过很多使用一个EditText的属性来控制键盘,例如类型的例子
The login screen consists of only a PIN style code field where the user will enter 4 digits. The problem is that by default it shows the regular alpha keyboard and I want it to show the numeric keyboard instead. I've seen lots of examples of using an EditText's properties to control the type of keyboard e.g.
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);
但有可能告诉键盘本身是什么类型的显示或设置web视图属性?
But is it possible to tell the keyboard itself what type to show or set the attribute on the WebView?
推荐答案
这条航线为我工作。创建的WebView
的子类,并重写方法 onCreateInputConnection
。这种方法被调用时,在的WebView的输入域
被选中,和它给你一个机会,自定义如何处理事件。这里有一个例子:
This route worked for me. Create a subclass of WebView
, and override the method onCreateInputConnection
. This method gets called when an input field in the WebView
is selected, and it gives you an opportunity to customize how the event is handled. Here's an example:
@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;
}
这篇关于在web视图显示数字键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!