[英]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.