Android 如何在webview中按go按钮时关闭键盘

Android 如何在webview中按go按钮时关闭键盘,android,webview,android-softkeyboard,Android,Webview,Android Softkeyboard,我有一个webview,它加载一个网页(启用了javascript,该网页和我用来直接从webview打开相机的活动之间有一个接口)。 我在那个网页上有几个字段,有些是数字,有些是纯文本。每个字段都有不同的键盘布局(尽管两者都有go按钮) 目前,Go按钮没有任何作用。我需要在点击键盘时关闭键盘 我发现这是关于如何实现这一点,但不幸的是,这个解决方案不适合我,因为我有不同的键盘布局。当我应用这个解决方案时,所有字段都得到数字键盘布局。 或字母,取决于输出属性。输入类型 这是我目前的代码: publ

我有一个webview,它加载一个网页(启用了javascript,该网页和我用来直接从webview打开相机的活动之间有一个接口)。 我在那个网页上有几个字段,有些是数字,有些是纯文本。每个字段都有不同的键盘布局(尽管两者都有go按钮)

目前,Go按钮没有任何作用。我需要在点击键盘时关闭键盘

我发现这是关于如何实现这一点,但不幸的是,这个解决方案不适合我,因为我有不同的键盘布局。当我应用这个解决方案时,所有字段都得到数字键盘布局。 或字母,取决于输出属性。输入类型

这是我目前的代码:

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;

    }

}

因此,这个解决方案演化出许多步骤,既有本机步骤,也有角度步骤

  • 需要在键盘上创建自定义webview以模拟输入(操作按钮)以替换为“完成”操作

    公共类WebViewGo扩展了WebView{

    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;
    
    }
    
    }

  • 需要在活动/片段中使用此webview而不是常规webview

  • 创建自定义javascript接口

    私有类WebAppInterface{

        @JavascriptInterface
        public void dismissKeyboard()
        {
            InputMethodManager imm = (InputMethodManager) TodoItemDetailActivity.this.getSystemService(Activity.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(webview.getWindowToken(), 0);
        }
    
    }
    
  • 将接口添加到webview

    webview.addJavascriptInterface(new WebAppInterface(), "Android");
    
  • 诀窍在于,当用户点击enter键时,他就进入了webview(这意味着它的焦点在网页上(angualr)

  • 当用户单击此字段中的“完成”时,我需要删除键盘

      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界面从活动/片段中关闭键盘。

    因此,该解决方案改进了许多步骤,其中包括都是土生土长的,棱角分明的

  • 需要在键盘上创建自定义webview以模拟输入(操作按钮)以替换为“完成”操作

    公共类WebViewGo扩展了WebView{

    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;
    
    }
    
    }

  • 需要在活动/片段中使用此webview而不是常规webview

  • 创建自定义javascript接口

    私有类WebAppInterface{

        @JavascriptInterface
        public void dismissKeyboard()
        {
            InputMethodManager imm = (InputMethodManager) TodoItemDetailActivity.this.getSystemService(Activity.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(webview.getWindowToken(), 0);
        }
    
    }
    
  • 将接口添加到webview

    webview.addJavascriptInterface(new WebAppInterface(), "Android");
    
  • 诀窍在于,当用户点击enter键时,他就进入了webview(这意味着它的焦点在网页上(angualr)

  • 当用户单击此字段中的“完成”时,我需要删除键盘

      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接口从活动/片段中关闭键盘