簡體   English   中英

如何從軟鍵盤捕捉“完成”按鍵

[英]How to catch a “Done” key press from the soft keyboard

如何從軟鍵盤捕獲特定的按鍵事件? 特別是我對“完成”鍵感興趣。

我不太確定在接受的答案中使用了哪種聽眾。 我使用附加到EditTextOnKeyListener並且它無法捕捉下一個或完成。

但是,使用OnEditorActionListener有效,它還允許我通過將操作值與定義的常量EditorInfo.IME_ACTION_NEXTEditorInfo.IME_ACTION_DONE進行比較來區分它們。

editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if ((actionId & EditorInfo.IME_MASK_ACTION) != 0) {
            doSomething();
            return true;
        }
        else {
            return false;
        }
    }
});

@Swato 的答案對我來說並不完整(並且無法編譯!)所以我展示了如何與 DONE 和 NEXT 操作進行比較。

editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
        int result = actionId & EditorInfo.IME_MASK_ACTION;
        switch(result) {
        case EditorInfo.IME_ACTION_DONE:
            // done stuff
            break;
        case EditorInfo.IME_ACTION_NEXT:
            // next stuff
            break;
        }
    }
});

另外我想指出,對於 JellyBean 和更高版本的 OnEditorActionListener 需要偵聽“輸入”或“下一步”,並且您不能使用 OnKeyListener。 從文檔:

由於軟輸入法可以使用多種創造性的文本輸入方式,因此不能保證軟鍵盤上的任何按鍵按下都會生成按鍵事件:這由 IME 自行決定,實際上不鼓勵發送此類事件 對於軟輸入法上的任何鍵,您永遠不應該依賴接收 KeyEvents。

參考: http : //developer.android.com/reference/android/view/KeyEvent.html

注意:這個答案是舊的,不再有效。 請參閱下面的答案。

您捕獲 KeyEvent,然后檢查其鍵碼。 FLAG_EDITOR_ACTION 用於識別來自 IME 的輸入鍵,輸入鍵已自動標記為“下一步”或“完成”

if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
    //your code here

此處查找文檔。

就這樣做:

editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    if(actionId == EditorInfo.IME_ACTION_DONE)
    {
       //Do Something
    }

    return false;
}
});
    etSearchFriends = (EditText) findViewById(R.id.etSearchConn);
    etSearchFriends.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // If the event is a key-down event on the "enter" button
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                (keyCode == KeyEvent.KEYCODE_ENTER)) {
                Toast.makeText(ACTIVITY_NAME.this, etSearchFriends.getText(),Toast.LENGTH_SHORT).show();
              return true;
            }
            return false;
        }

    }); 

要從軟鍵盤中捕獲“完成”按鍵,請覆蓋 Activity 的 onKeyUp 方法。 為視圖設置 OnKeyListener 偵聽器將不起作用,因為軟件輸入法中的按鍵按下通常不會觸發此偵聽器的方法,當在視圖中按下硬件鍵時會調用此回調。

// Called when a key was released and not handled by any of the views inside of the activity. 
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_ENTER:
            // code here
            break;
        default:
            return super.onKeyUp(keyCode, event);
    }
    return true;
}

注意:在您的編輯文本中提及輸入類型。

<EditText android:id="@+id/select_category" 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" >

edittext.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if ((actionId & EditorInfo.IME_MASK_ACTION) == EditorInfo.IME_ACTION_DONE) {
                    //do something here.
                    return true;
                }
                return false;
            }
        });

您可以通過此方法覆蓋完成的鍵事件:

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // do your stuff here
        }
        return false;
    }
});

我有搜索名稱的 EditText,它會自動在 ListView 中顯示下面的結果。 SoftInput 鍵盤只顯示“下一步”按鈕並輸入符號 - 這沒有做任何事情。 我只想要完成按鈕(沒有下一個或輸入符號),而且我想要它被按下時,它應該關閉鍵盤,因為用戶應該看到它下面的結果。

我發現/由 Cyril Mottier 先生在他的博客上找到的解決方案非常簡單,它無需任何額外代碼即可工作:在 EditText 所在的 xml 中,這應該寫成:android:imeOptions="actionDone"

所以用“完成”按鈕隱藏鍵盤,EditText 應該是這樣的:

<EditText
        android:id="@+id/editText1central"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView1"
        android:layout_toLeftOf="@+id/imageView2tie"
        android:ems="10"
        android:imeOptions="actionDone"
        android:hint="@string/trazi"
        android:inputType="textPersonName" />

科特林版本:

<EditText android:id="@+id/edit_text" 
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:inputType="text" />

不要忘記設置android:inputType。

// Get reference to EditText.
val editText = findViewById<EditText>(R.id.edit_text)

editText.setOnEditorActionListener { _, actionId: Int, _ ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        // Do your logic here.
        true
    } else {
        false
    }
}

IME_MASK_ACTION是255,而收到的actionId是6,我的編譯器不接受

if (actionId & EditorInfo.IME_MASK_ACTION) 

這是一個整數。 &-ing 255 到底有什么用? 所以測試簡單地可以是

public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_DONE)
    ...
editText = (EditText) findViewById(R.id.edit_text);

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // code here
        }
        return false;
    }
});

暫無
暫無

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

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