在没有编辑文本的情况下捕获软键盘上完成的操作?
Capture done action on softkeyboard without edittext?
在我的应用程序中,我有一个显示一些内容的网络视图。其中一个屏幕有一个要填写的文本框。我想在用户按下键盘上的完成按钮时捕获,但也没有编辑文本来添加侦听器。无论设备和键盘如何捕捉动作?
我尝试过这些,但运气不佳。
EditText with textPassword inputType, but without Softkeyboard
android: Softkeyboard perform action when Done key is pressed
@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;
}
我的 class 覆盖了 KeyEvent.Callback 但上面的 onKeyDown 函数从未被调用。
按键事件几乎从不从软键盘发送,它们使用更直接的方法。
Android 上键盘的工作方式是绑定到视图。该视图必须实现 getInputConnection() returning 一个对象,该对象将允许键盘应用程序调用(通过 AIDL)函数。 "action key"(完成按钮)调用了其中一个函数。在默认的 InputConnection 实现中,这将调用注册到绑定视图的侦听器。
由于您在这里处理的是网络视图 - 我认为没有办法直接捕获它。您可以尝试将 WebView 子类化为 ActionKeyWebView。添加一个函数来注册一个动作键监听器接口。
将 getInputConnection 重写为 return 您自己的 InputConnectionWrapper 子类,并包装 super.getInputConnection()。然后覆盖 performEditorAction 以调用为 webview 注册的任何侦听器。它有相当多的代码,但它应该可以工作。
创建自定义 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 {
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
}
}
和XML
<com.example.MyWeb
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:id="@+id/web"
/>`
来源:https://medium.com/@elye.project/managing-keyboard-on-webview-d2e89109d106
在我的应用程序中,我有一个显示一些内容的网络视图。其中一个屏幕有一个要填写的文本框。我想在用户按下键盘上的完成按钮时捕获,但也没有编辑文本来添加侦听器。无论设备和键盘如何捕捉动作?
我尝试过这些,但运气不佳。 EditText with textPassword inputType, but without Softkeyboard
android: Softkeyboard perform action when Done key is pressed
@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;
}
我的 class 覆盖了 KeyEvent.Callback 但上面的 onKeyDown 函数从未被调用。
按键事件几乎从不从软键盘发送,它们使用更直接的方法。
Android 上键盘的工作方式是绑定到视图。该视图必须实现 getInputConnection() returning 一个对象,该对象将允许键盘应用程序调用(通过 AIDL)函数。 "action key"(完成按钮)调用了其中一个函数。在默认的 InputConnection 实现中,这将调用注册到绑定视图的侦听器。
由于您在这里处理的是网络视图 - 我认为没有办法直接捕获它。您可以尝试将 WebView 子类化为 ActionKeyWebView。添加一个函数来注册一个动作键监听器接口。 将 getInputConnection 重写为 return 您自己的 InputConnectionWrapper 子类,并包装 super.getInputConnection()。然后覆盖 performEditorAction 以调用为 webview 注册的任何侦听器。它有相当多的代码,但它应该可以工作。
创建自定义 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 {
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
}
}
和XML
<com.example.MyWeb
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:id="@+id/web"
/>`
来源:https://medium.com/@elye.project/managing-keyboard-on-webview-d2e89109d106