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

Android 疑難:HTC One S 上 SearchView 的軟鍵盤不吃 actionSearch

編輯:關於 Android 程式設計

前言

怪問題年年有,今年特別多。這次遇到的是 SearchView 在 HTC One S 上的行為異常,不處理就完全沒辦法在這台機器上正常使用 SearchView——它的軟鍵盤不認 searchable.xml 裡設定的 action。

正文

問題機型:HTC One S

原本 searchable.xml 的設定是這樣:

<searchable xmlns:android="https://schemas.android.com/apk/res/android"
    android:label="@string/search_label"
    android:hint="@string/search_hint"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
    android:imeOptions="actionSearch"
    android:searchSuggestAuthority="com.xxx.provider.searchsuggestion"
    android:searchSuggestSelection=" ? " />

解法

SearchView 提供了一個 setSubmitButtonEnabled 方法,把它設為 true 即可。這樣做的好處是:只要搜尋框不為空,就會出現一個明確的送出按鈕,不再依賴 IME 的 action 按鍵。效果如下:

呼叫時機是在 onCreateOptionsMenu 中 inflate 之後,另外記得加上 API Level 判斷,畢竟這是比較新才有的方法。

以上就是 HTC One S 上 SearchView 軟鍵盤不支援 actionSearch 的解法,希望能幫到開發 Android 軟體的朋友。

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