Android 如何在webview中按go按钮时关闭键盘
我有一个webview,它加载一个网页(启用了javascript,该网页和我用来直接从webview打开相机的活动之间有一个接口)。 我在那个网页上有几个字段,有些是数字,有些是纯文本。每个字段都有不同的键盘布局(尽管两者都有go按钮) 目前,Go按钮没有任何作用。我需要在点击键盘时关闭键盘 我发现这是关于如何实现这一点,但不幸的是,这个解决方案不适合我,因为我有不同的键盘布局。当我应用这个解决方案时,所有字段都得到数字键盘布局。 或字母,取决于输出属性。输入类型 这是我目前的代码:Android 如何在webview中按go按钮时关闭键盘,android,webview,android-softkeyboard,Android,Webview,Android Softkeyboard,我有一个webview,它加载一个网页(启用了javascript,该网页和我用来直接从webview打开相机的活动之间有一个接口)。 我在那个网页上有几个字段,有些是数字,有些是纯文本。每个字段都有不同的键盘布局(尽管两者都有go按钮) 目前,Go按钮没有任何作用。我需要在点击键盘时关闭键盘 我发现这是关于如何实现这一点,但不幸的是,这个解决方案不适合我,因为我有不同的键盘布局。当我应用这个解决方案时,所有字段都得到数字键盘布局。 或字母,取决于输出属性。输入类型 这是我目前的代码: publ
public class WebViewGo extends WebView {
public WebViewGo(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
BaseInputConnection inputConnection = new BaseInputConnection(this, false);
outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;
outAttrs.inputType = EditorInfo.TYPE_CLASS_NUMBER;
return inputConnection;
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
boolean isDispached = super.dispatchKeyEvent(event);
if(event.getAction() == KeyEvent.ACTION_UP){
switch (event.getKeyCode())
{
case KeyEvent.KEYCODE_ENTER:
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
break;
}
}
return isDispached;
}
}
我是否可以在保留多个键盘布局的同时使用此解决方案,或者是否有其他解决方案来解决此问题
先谢谢你
更新
我完全忘了提到我试图删除onCreateInputConnection,但没有它,dispatchKeyEvent似乎无法工作
更新2
由于某些原因,在使用文本字段时无法正常工作,我成功地将带有Dislose功能的“完成”按钮添加到数字字段中
这是迄今为止我的自定义webview代码
public class WebViewGo extends WebView {
public WebViewGo(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
InputConnection connection = super.onCreateInputConnection(outAttrs);
outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;
if ((outAttrs.inputType & InputType.TYPE_CLASS_NUMBER) == InputType.TYPE_CLASS_NUMBER)
{
connection = new BaseInputConnection(this, false);
}else{
outAttrs.inputType = EditorInfo.TYPE_CLASS_TEXT;
}
return connection;
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
boolean isDispached = super.dispatchKeyEvent(event);
if(event.getAction() == KeyEvent.ACTION_UP){
Log.d("anton","dispatchKeyEvent="+event.getKeyCode());
switch (event.getKeyCode())
{
case KeyEvent.KEYCODE_ENTER:
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
break;
}
}
return isDispached;
}
}
因此,这个解决方案演化出许多步骤,既有本机步骤,也有角度步骤
public WebViewGo(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
InputConnection connection = super.onCreateInputConnection(outAttrs);
if((outAttrs.imeOptions & EditorInfo.IME_ACTION_GO)==EditorInfo.IME_ACTION_GO ||
(outAttrs.imeOptions & EditorInfo.IME_ACTION_NONE)==EditorInfo.IME_ACTION_NONE)
{
outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;
}
return connection;
}
} @JavascriptInterface
public void dismissKeyboard()
{
InputMethodManager imm = (InputMethodManager) TodoItemDetailActivity.this.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(webview.getWindowToken(), 0);
}
}
webview.addJavascriptInterface(new WebAppInterface(), "Android");
onKeyUp(event : any){
if (event.key === 'Enter') {
if (typeof Android !== 'undefined' && Android) {
event.target.value = event.target.value.replace(/\n/g, "");
//console.log(event.target.value);
Android.dismissKeyboard();
}
}
}
我遇到的另一个小问题是,尽管我成功地关闭了键盘,但它仍然会在文本末尾添加enter,并且我需要删除它,在这之后,只需调用本机函数,即可使用javascript界面从活动/片段中关闭键盘。因此,该解决方案改进了许多步骤,其中包括都是土生土长的,棱角分明的
public WebViewGo(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
InputConnection connection = super.onCreateInputConnection(outAttrs);
if((outAttrs.imeOptions & EditorInfo.IME_ACTION_GO)==EditorInfo.IME_ACTION_GO ||
(outAttrs.imeOptions & EditorInfo.IME_ACTION_NONE)==EditorInfo.IME_ACTION_NONE)
{
outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;
}
return connection;
}
} @JavascriptInterface
public void dismissKeyboard()
{
InputMethodManager imm = (InputMethodManager) TodoItemDetailActivity.this.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(webview.getWindowToken(), 0);
}
}
webview.addJavascriptInterface(new WebAppInterface(), "Android");
onKeyUp(event : any){
if (event.key === 'Enter') {
if (typeof Android !== 'undefined' && Android) {
event.target.value = event.target.value.replace(/\n/g, "");
//console.log(event.target.value);
Android.dismissKeyboard();
}
}
}
我遇到的另一个小问题是,尽管我成功地关闭了键盘,但它仍然会在文本末尾添加enter,并且我需要删除它,在这之后,只需调用本机函数即可使用javascript接口从活动/片段中关闭键盘