简体   繁体   English

在没有edittext的情况下在软键盘上捕获完成的动作?

[英]Capture done action on softkeyboard without edittext?

In my app I have a webview that displays some content. 在我的应用程序中,我有一个显示某些内容的Web视图。 One of these screen has a text box to fill in. I want to capture when the user presses the done button on the keyboard but there's no edit text to add a listener too. 其中一个屏幕上有一个文本框可填写。我想捕获用户何时按下键盘上的完成按钮,但是也没有添加编辑器的文本来添加侦听器。 How can I capture the action regardless of device & keyboard? 无论设备和键盘如何,如何捕获动作?

I've tried these with little luck. 我很幸运地尝试了这些。 EditText with textPassword inputType, but without Softkeyboard 带有textPassword inputType的EditText,但没有软键盘

android: Softkeyboard perform action when Done key is pressed android:按下完成键时,软键盘执行操作

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

My class overrides KeyEvent.Callback but the above funtion onKeyDown is never called. 我的类重写KeyEvent.Callback,但从未调用上述功能onKeyDown。

Create a custom webview, override onCreateInputConnection to set ime option and input type to keyboard, override dispatchKeyEvent to get the key events filter it out 创建一个自定义的webview,重写onCreateInputConnection以将ime选项和输入类型设置为键盘,重写dispatchKeyEvent以获取将其过滤掉的键事件

Example : 范例:

class MyWeb@JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0) : WebView(context, attrs, defStyleAttr) {

override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection {
    val inputConnection = BaseInputConnection(this, false)
    return inputConnection
}

override fun dispatchKeyEvent(event: KeyEvent): Boolean {
    super.dispatchKeyEvent(event)
    val dispatchFirst = super.dispatchKeyEvent(event)
    if (event.action == KeyEvent.ACTION_UP) {
        when (event.keyCode) {
            KeyEvent.KEYCODE_ENTER -> {
                Toast.makeText(context,"Hii",Toast.LENGTH_LONG).show()
                //callback?.onEnter()
            }
        }
    }
    return dispatchFirst
}

} }

and XML 和XML

<com.example.MyWeb
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:id="@+id/web"
/>`

Source : https://medium.com/@elye.project/managing-keyboard-on-webview-d2e89109d106 来源: https : //medium.com/@elye.project/managing-keyboard-on-webview-d2e89109d106

Key events are almost never sent from a soft keyboard, they use more direct methods. 按键事件几乎从不从软键盘发送,它们使用更直接的方法。

The way a keyboard on Android works is its bound to a view. Android键盘的工作方式是绑定到视图。 That view has to implement getInputConnection() returning an object that will allow functions to be called (via AIDL) by the keyboard app. 该视图必须实现getInputConnection()返回一个对象,该对象将允许键盘应用程序(通过AIDL)调用函数。 One of these functions is called for the "action key" (the done button). 这些功能之一被称为“动作键”(完成按钮)。 In the default InputConnection implementation, that will call a listener registered to the bound view. 在默认的InputConnection实现中,它将调用注册到绑定视图的侦听器。

Since you're dealing with a webview here- I don't think there is a way to capture it directly. 由于您是在这里处理网络视图-我认为没有办法直接捕获它。 What you can try is to subclass WebView to ActionKeyWebView. 您可以尝试将WebView子类化为ActionKeyWebView。 Add a function to register an action key listener interface. 添加一个功能来注册操作键侦听器接口。 Override getInputConnection to return your own InputConnectionWrapper subclass, and wrap super.getInputConnection(). 重写getInputConnection以返回您自己的InputConnectionWrapper子类,并包装super.getInputConnection()。 THen override performEditorAction to call any listener registered for the webview. 重写performEditorAction可以调用为Webview注册的所有侦听器。 Its a fair amount of code but it should work. 它有很多代码,但是应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android中显示软键盘时,edittext在错误的位置 - edittext are in the wrong place when softkeyboard are shown in android 如何避免软键盘与片段中的 EditText 重叠? - How to avoid the softkeyboard overlapping the EditText in fragment? 在不按按钮的情况下在一个编辑文本中完成输入的情况下,将输入打印为另一个编辑文本 - Print input in another edittext while input is done in one edittext without a button press Android:有没有办法确定在edittext软键盘上按键/按下键的时间? - Android: Is there any way to identify when the keys are pressed/unpressed on an edittext softkeyboard? 显示软键盘时如何在Android中获取Edittext输入类型 - How to get the Edittext input type in Android when softkeyboard is displayed Android-如果按键时EditText为空,如何隐藏软键盘? - Android - How to hide softkeyboard if EditText is empty on key press? 隐藏或禁用WebView软键盘“ Go / Done / Enter”按钮 - Hide or disable WebView softkeyboard Go/Done/Enter button 防止软键盘显示在包含edittext的弹出窗口中? - prevent softkeyboard from showing up in popup's which contain edittext? 在操作栏上添加 editText - Add editText on action bar 单击editText后,转到另一个edittext时完成 - After click on editText is done when going to another edittext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM