如何在网络视图中按下执行按钮时关闭键盘

问题描述 投票:0回答:1

我有一个加载了网页的Web视图(启用了JavaScript,并且该页面与我用来直接从Web视图中打开相机的活动之间存在接口)。我在该网页中有几个字段,其中一些是数字,一些是纯文本。每个字段都有不同的键盘布局(尽管两个都有go按钮)。

当前,“转到”按钮没有执行任何操作。我需要在单击键盘时将其关闭。

我找到了如何实现此目标的tutorial,但不幸的是,此解决方案不适用于我,因为我的键盘布局不同。当我应用此解决方案时,所有字段都将获得数字键盘的布局。或取决于outAttrs.inputType

的字母

到目前为止,这是我的代码:

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


}

我可以在保持几个键盘布局的同时使用此解决方案,还是对此问题有其他解决方案。

先谢谢您。

UPDATE

我完全忘记提及我试图删除onCreateInputConnection,但是没有它,似乎dispatchKeyEvent无法正常工作。

UPDATE 2

由于某些原因,我设法将具有关闭功能的完成按钮添加到数字字段中,原因是在使用文本字段时它无法正常工作

到目前为止,这是我的自定义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;

    }

}
android webview android-softkeyboard
1个回答
0
投票

因此,该解决方案发展了许多本机的和有角度的步骤。

  1. 需要创建自定义Web视图来模拟键盘中的Enter(操作按钮)以替换为“完成”操作

    公共类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;
    
    }
    

    }

需要在活动/片段中使用此Web视图而不是常规Web视图

  1. 创建自定义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");
  1. 诀窍是,当用户单击Enter时,他位于Web视图中(意味着其集中在Web页面中(角度)

当用户在此字段中单击“完成”时,我需要卸下键盘。

  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();

      }
    }
  }

我遇到的另一个小问题是,即使我设法关闭了键盘,它仍然会在文本的末尾添加回车键,因此我需要将其删除,这很简单,只需调用将键盘从使用javascript界面​​的活动/片段。

© www.soinside.com 2019 - 2024. All rights reserved.