1. 程式人生 > >Android開發 匹配軟鍵盤迴車鍵

Android開發 匹配軟鍵盤迴車鍵

在android開發過程中,有時候需要對EditText的軟鍵盤進行監聽。當點選軟鍵盤迴車位置按鍵的時候,需要實現 完成、前進、下一項、搜尋、傳送或其他功能。這就需要開發者對軟鍵盤迴車的點選事件進行捕捉。

1. 我們需要先在XML檔案中設定EditText的 android:imeOptions=""屬性,
(IME英文全稱Input Method Editors,中文名稱輸入法編輯器)

  <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
android:imeOptions="actionSearch" android:singleLine="true" /> 注意: 需要設定`android:inputType="text"`或`android:singleLine="true"`, 否則程式碼中的`setOnEditorActionListener`可能不起作用。
android:imeOptions=""的XML屬性值 對應 EditorInfo 常量值
actionSearch EditorInfo.IME_ACTION_SEARCH
actionSend EditorInfo.IME_ACTION_SEND
actionNext EditorInfo.IME_ACTION_NEXT
actionGo EditorInfo.IME_ACTION_GO
actionDone EditorInfo.IME_ACTION_DONE
actionNone EditorInfo.IME_ACTION_NONE
actionPrevious EditorInfo.IME_ACTION_PREVIOUS
actionUnspecified EditorInfo.IME_ACTION_UNSPECIFIED

2.在程式碼中對EditText設定setOnEditorActionListener監聽事件。

 editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            /****
             * 
             * @param v 可以理解為是向上轉型的EditText,可以用來操作當前的EditText
             * @param actionId 動作標識,是跟EditorInfo.IME_**這裡的值對比可以判斷執行了什麼動作
             * @param event  跟KeyEvent.ACTION_**比較值判斷它的事件
             * @return  如果不往下執行到此結束,返回true,否則為false。
             */
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                 if (actionId == EditorInfo.IME_ACTION_SEARCH) {//判斷動作標識是否匹配
                      // To do something
                }
                return false;
            }
        });

上面是對軟鍵盤迴車鍵匹配的步驟,總共就兩步,其實挺簡單的。
以下是在MIUI系統中搜狗輸入法小米版對Enter鍵的匹配樣式。不同的系統,不同的輸入法,可能會對Enter鍵的匹配不同,其他輸入法請自行測試。

這裡寫圖片描述
這裡寫圖片描述
這裡寫圖片描述
這裡寫圖片描述
這裡寫圖片描述
這裡寫圖片描述
這裡寫圖片描述
這裡寫圖片描述

八個屬性的原始碼+註釋。

/**
     * Bits of {@link #IME_MASK_ACTION}: no specific action has been
     * associated with this editor, let the editor come up with its own if
     * it can.
     */
    public static final int IME_ACTION_UNSPECIFIED = 0x00000000;

    /**
     * Bits of {@link #IME_MASK_ACTION}: there is no available action.
     */
    public static final int IME_ACTION_NONE = 0x00000001;

    /**
     * Bits of {@link #IME_MASK_ACTION}: the action key performs a "go"
     * operation to take the user to the target of the text they typed.
     * Typically used, for example, when entering a URL.
     */
    public static final int IME_ACTION_GO = 0x00000002;

    /**
     * Bits of {@link #IME_MASK_ACTION}: the action key performs a "search"
     * operation, taking the user to the results of searching for the text
     * they have typed (in whatever context is appropriate).
     */
    public static final int IME_ACTION_SEARCH = 0x00000003;

    /**
     * Bits of {@link #IME_MASK_ACTION}: the action key performs a "send"
     * operation, delivering the text to its target.  This is typically used
     * when composing a message in IM or SMS where sending is immediate.
     */
    public static final int IME_ACTION_SEND = 0x00000004;

    /**
     * Bits of {@link #IME_MASK_ACTION}: the action key performs a "next"
     * operation, taking the user to the next field that will accept text.
     */
    public static final int IME_ACTION_NEXT = 0x00000005;

    /**
     * Bits of {@link #IME_MASK_ACTION}: the action key performs a "done"
     * operation, typically meaning there is nothing more to input and the
     * IME will be closed.
     */
    public static final int IME_ACTION_DONE = 0x00000006;

    /**
     * Bits of {@link #IME_MASK_ACTION}: like {@link #IME_ACTION_NEXT}, but
     * for moving to the previous field.  This will normally not be used to
     * specify an action (since it precludes {@link #IME_ACTION_NEXT}), but
     * can be returned to the app if it sets {@link #IME_FLAG_NAVIGATE_PREVIOUS}.
     */
    public static final int IME_ACTION_PREVIOUS = 0x00000007;

相關推薦

Android開發 匹配鍵盤

在android開發過程中,有時候需要對EditText的軟鍵盤進行監聽。當點選軟鍵盤迴車位置按鍵的時候,需要實現 完成、前進、下一項、搜尋、傳送或其他功能。這就需要開發者對軟鍵盤迴車的點選事件進行捕捉。 1. 我們需要先在XML檔案中設定EditText的

android呼叫輸入鍵盤跟刪除

android EditText控鍵在呼叫軟鍵盤的時候功能實現: 1、Editext 實現繫結輸入法回車鍵程式碼實現     password.setOnEditorActionListener(new EditText.OnEditorActionListener() {

android監聽鍵盤並且解決預設點選兩次的問題

@Override public boolean dispatchKeyEvent(KeyEvent event) { if(event.getKeyCode()==KeyEvent.KEYCODE_ENTER&&event.getActio

Android知識點——更改鍵盤

積跬步,以至千里;積小流,以成江海。 場景:當點選搜尋框,或者聊天訊息框是,會看到軟鍵盤的回車鍵變成“搜尋”或者“傳送”提醒。 實現如上需求,必然需要使用EditText的幾個屬性 android:imeActionId=”3” and

Android如何將鍵盤換成搜尋等按鈕,EditText中imeOptions屬性的使用

一、效果圖 如上圖,在使用鍵盤輸入的時候,有時我們可以看到回車鍵是“下一步”、“搜尋”、“確認”等,那麼這個效果要怎麼做呢?其實很簡單,我們只需要在EditText中設定imeOptions這個屬性就行了。     <EditText         androi

Android監聽鍵盤事件

在Android開發中,難免會碰到一些”意外“。比如輸入法軟按鍵監聽問題,因為第三方輸入法各有不同(對一些按鍵事件作了一些特殊的處理),所以有時有些程式碼會“失靈”。假設一個場景,EditText監聽回車事件,回車後就傳送輸入的內容,一般有以下4種處理方式: 假設場景圖:

解決Android EditText或者AppCompatEditText按鍵盤只是換行的問題

Android中 使用EditText或者android.support.v7.widget.AppCompatEditText輸入時 預設的軟鍵盤的回車鍵只是用於換行, 這會導致使用者誤按回車導致EditTex輸入區域變寬,UI變形的BUG情況, 度娘上的回答都是讓設定EditText

Android】EditText實現搜尋功能,把鍵盤改為搜尋;EditText隱藏游標

1、xml檔案中設定屬性 android:imeOptions="actionSearch"                 android:singleLine="true" 注:android:singleLine 已過期,不過設定為maxlines = 1  不會將回

Android源碼開發筆記 鍵盤與內置物理鍵盤共存以及外接藍牙鍵盤不共存邏輯

icon eva boolean 功能實現 小鍵盤 nokey top apps 需求 需求1: android設備自帶九鍵的小鍵盤,此時小鍵盤被識別為HW Keyboard,默認與軟鍵盤不能共存,需要使軟鍵盤與物理鍵盤共存。 實現: 在網上找的別人總結的Android5.

Android Edittext改變鍵盤為搜尋

做搜尋功能的時候遇到的坑,網上都是android:imeOptions=”actionSearch”,但是對Edittext不起作用,鍵盤上的回車鍵不變。 android:singleLine="true" android:imeOptions="actionSear

Android 系列 6 21在鍵盤上將輸入更改為 下一步

lan class 技術 大小寫 Coding 模式 調焦 nta 進行 6.21在軟鍵盤上將輸入鍵更改為“下一步” 問題 包括Web瀏覽器和聯系人應用程序在內的多個應用程序用下一個鍵替換屏幕鍵盤上的Enter鍵,以關註下一個數據輸入視圖。您希

Android:隱藏輸入法鍵盤

內部 encoding utf-8 res ger 裏的 trac mini 對象 1.概述 1) 給LinearLayout 註冊click 事件,點擊後隱藏輸入法軟鍵盤。為什麽要叫軟鍵盤,很奇怪呢。通過Activity 的getSystem(Context.INPUT_

android-func-隱藏鍵盤

/** * 隱藏軟鍵盤 */ private void hideInputWindow() { ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)) .hideSoftInputFromWi

android 禁止系統鍵盤,攔截鍵盤事件

在Edittext中設定 .setInputType(InputType.TYPE_NULL); public void disableShowInput(){ if (android.os.Build.VERSION.SDK_INT <= 10){ editText.setIn

vue 鍵盤事件

如果是原生的input,使用 @keyup.enter就可以,若是使用了element-ui,則要加上native限制符,因為element-ui把input進行了封裝,原事件就不起作用了,程式碼如下: <input v-model="form.name" placeholder="暱稱" @keyu

android 監聽鍵盤在頁面的展開和隱藏

獲取軟鍵盤狀態思路: 獲取當前頁面根佈局及其高度 RootH; 獲取狀態列高度 StatusH和導航欄高度 NavigationH; 獲取當前根檢視在螢幕上顯示的高度RectH; 高度差值比較,(根佈局高度 - 根檢視顯示高度)與(狀態列高度 + 導航欄高度)的大小對

Android彈出鍵盤和關閉鍵盤

在一些有輸入框的介面中,我們需要自動彈出軟鍵盤。比如搜尋介面,輸入框需要自動獲取焦點並彈出軟鍵盤。 彈出軟鍵盤程式碼: mEditText.setFocusable(true); mEdit

vue+element Form鍵盤事件頁面重新整理解決

問題描述:如下程式碼所示,使用element-ui 中的el-form元件對table進行條件查詢,當查詢條件僅有一項時,使用@keyup.enter.native事件繫結回車事件,出現點選回車後,瀏覽器會重新整理頁面的問題; &lt;el-form :inline="true" class="

Android 監聽鍵盤狀態

近日遇到要檢測軟鍵盤是否顯示或隱藏的問題,搜了一下網上,最後找到一個很簡單的,記錄一下。 activityRoot是activity的根view,就是xml裡面的第一個view,給它設定一個id. final View activityRootView = findView

Android筆記:鍵盤彈出遮蓋原來介面的佈局控制元件

給Activity設定軟鍵盤出現與Activity之間的互動模式: 1.在onCreat中的setContent方法之前寫入: getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJ