簡體   English   中英

在沒有edittext的情況下在軟鍵盤上捕獲完成的動作?

[英]Capture done action on softkeyboard without edittext?

在我的應用程序中,我有一個顯示某些內容的Web視圖。 其中一個屏幕上有一個文本框可填寫。我想捕獲用戶何時按下鍵盤上的完成按鈕,但是也沒有添加編輯器的文本來添加偵聽器。 無論設備和鍵盤如何,如何捕獲動作?

我很幸運地嘗試了這些。 帶有textPassword inputType的EditText,但沒有軟鍵盤

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

我的類重寫KeyEvent.Callback,但從未調用上述功能onKeyDown。

創建一個自定義的webview,重寫onCreateInputConnection以將ime選項和輸入類型設置為鍵盤,重寫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

按鍵事件幾乎從不從軟鍵盤發送,它們使用更直接的方法。

Android鍵盤的工作方式是綁定到視圖。 該視圖必須實現getInputConnection()返回一個對象,該對象將允許鍵盤應用程序(通過AIDL)調用函數。 這些功能之一被稱為“動作鍵”(完成按鈕)。 在默認的InputConnection實現中,它將調用注冊到綁定視圖的偵聽器。

由於您是在這里處理網絡視圖-我認為沒有辦法直接捕獲它。 您可以嘗試將WebView子類化為ActionKeyWebView。 添加一個功能來注冊操作鍵偵聽器接口。 重寫getInputConnection以返回您自己的InputConnectionWrapper子類,並包裝super.getInputConnection()。 重寫performEditorAction可以調用為Webview注冊的所有偵聽器。 它有很多代碼,但是應該可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM