在没有编辑文本的情况下捕获软键盘上完成的操作?

在我的应用程序中,我有一个显示一些内容的网络视图。其中一个屏幕有一个要填写的文本框。我想在用户按下键盘上的完成按钮时捕获,但也没有编辑文本来添加侦听器。无论设备和键盘如何,我如何捕捉动作?

@Override

     public boolean onKeyDown(int keyCode, KeyEvent event) {

        switch (keyCode) {

            case KeyEvent.KEYCODE_ENTER:

                // code here

                break;

            default:

                return super.onKeyUp(keyCode, event);

        }

        return true;

    }

我的班级重写了 KeyEvent.Callback 但从未调用过上面的 onKeyDown 函数。


杨魅力
浏览 71回答 2
2回答

慕哥6287543

创建自定义 webview,覆盖 onCreateInputConnection 以设置输入时间选项和键盘输入类型,覆盖 dispatchKeyEvent 以获取关键事件将其过滤掉例子 :class MyWeb@JvmOverloads constructor(context: Context,attrs: AttributeSet? = null,defStyleAttr: Int = 0) : WebView(context, attrs, defStyleAttr) {override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection {&nbsp; &nbsp; val inputConnection = BaseInputConnection(this, false)&nbsp; &nbsp; return inputConnection}override fun dispatchKeyEvent(event: KeyEvent): Boolean {&nbsp; &nbsp; super.dispatchKeyEvent(event)&nbsp; &nbsp; val dispatchFirst = super.dispatchKeyEvent(event)&nbsp; &nbsp; if (event.action == KeyEvent.ACTION_UP) {&nbsp; &nbsp; &nbsp; &nbsp; when (event.keyCode) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KeyEvent.KEYCODE_ENTER -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(context,"Hii",Toast.LENGTH_LONG).show()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //callback?.onEnter()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return dispatchFirst}}和XML<com.example.MyWeb&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:focusable="true"&nbsp; &nbsp; &nbsp; &nbsp; android:focusableInTouchMode="true"&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/web"/>`资料来源:https ://medium.com/@elye.project/managing-keyboard-on-webview-d2e89109d106

海绵宝宝撒

按键事件几乎从不从软键盘发送,它们使用更直接的方法。Android 键盘的工作方式是绑定到视图。该视图必须实现 getInputConnection() 返回一个对象,该对象将允许键盘应用程序调用(通过 AIDL)函数。这些功能之一称为“操作键”(完成按钮)。在默认的 InputConnection 实现中,这将调用注册到绑定视图的侦听器。由于您在这里处理网络视图 - 我认为没有办法直接捕获它。您可以尝试将 WebView 子类化为 ActionKeyWebView。添加一个函数来注册一个动作键监听器接口。覆盖 getInputConnection 以返回您自己的 InputConnectionWrapper 子类,并包装 super.getInputConnection()。然后覆盖 performEditorAction 以调用为 webview 注册的任何侦听器。它有相当多的代码,但它应该可以工作。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java