Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 監聽軟鍵盤按鍵的三種方式與自訂「完成鍵」樣式

編輯:關於 Android 開發

在 Android 中,輸入框右下角的按鈕樣式是可以自訂的,搭配監聽器就能實作搜尋、下一步、送出等互動。本文整理常見的 imeOptions 取值與監聽方式。

一、常見的 imeOptions 取值

  • actionNone:回車鍵,按下後游標移到下一行。
  • actionGo:顯示 Go。
  • actionSearch:顯示放大鏡圖示。
  • actionSend:顯示 Send。
  • actionNext:顯示 Next。
  • actionDone:顯示 Done,代表確定/完成,會收起軟鍵盤,即使不是最後一個輸入框也一樣。

二、在版面檔中設定按鈕樣式

關鍵是 android:imeOptions,通常還要搭配 android:singleLine="true" 才能正常生效:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleline="true"
    android:imeoptions="actionSearch" />

三、方式一:使用 OnEditorActionListener 監聽

為 EditText 設定 setOnEditorActionListener(),即可攔截軟鍵盤的動作按鈕:

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    boolean isOK = true;
    switch (actionId) {
        case EditorInfo.IME_ACTION_NONE:
            Toast.makeText(mContext, "點擊-->NONE", Toast.LENGTH_SHORT).show();
            break;
        case EditorInfo.IME_ACTION_GO:
            Toast.makeText(mContext, "點擊-->GO", Toast.LENGTH_SHORT).show();
            break;
        case EditorInfo.IME_ACTION_SEARCH:
            Toast.makeText(mContext, "點擊-->SEARCH", Toast.LENGTH_SHORT).show();
            break;
        case EditorInfo.IME_ACTION_SEND:
            Toast.makeText(mContext, "點擊-->SEND", Toast.LENGTH_SHORT).show();
            break;
        case EditorInfo.IME_ACTION_NEXT:
            Toast.makeText(mContext, "點擊-->NEXT", Toast.LENGTH_SHORT).show();
            break;
        default:
            isOK = false;
            break;
    }
    return isOK;
}

回傳 true 表示事件已消費完畢,系統不會再執行預設行為(例如再多換一行);回傳 false 則交給系統處理。

四、方式二:使用 OnKeyListener 監聽

若需要取得完整的按鍵事件(含 KeyEvent),可改用:

et.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // 建議同時判斷 KEYCODE_ENTER 與 KEYCODE_NUMPAD_ENTER
        if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            // 在這裡處理邏輯
            return true;
        }
        return false;
    }
});

五、方式三:使用 TextWatcher 攔截換行

有些輸入法在單行模式下並不會觸發上述監聽器,這時可以退而求其次,用 TextWatcher 監聽內容變化,偵測到換行符號時自行攔截:

et.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable s) {
        if (s.toString().contains("\n")) {
            // 移除換行並執行自訂邏輯
        }
    }
    // beforeTextChanged、onTextChanged 可留空
});

六、完整版面示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.edwin.demokeyboard.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="改變軟鍵盤右下角確定鍵樣式"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/et_main_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="actionGo"
        android:imeOptions="actionGo"
        android:singleLine="true" />

    <EditText
        android:id="@+id/et_main_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="actionSearch"
        android:imeOptions="actionSearch"
        android:singleLine="true" />

    <EditText
        android:id="@+id/et_main_three"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="actionSend"
        android:imeOptions="actionSend"
        android:singleLine="true" />

    <EditText
        android:id="@+id/et_main_four"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="actionNext"
        android:imeOptions="actionNext"
        android:singleLine="true" />

</LinearLayout>

實際效果會因機型與輸入法而異,建議至少同時實作 OnEditorActionListener 與 OnKeyListener,以覆蓋絕大多數情境。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved