Android讓EditText輸入的時候,彈出的虛擬鍵盤的回車變成“搜索”

java代碼設置:

mEditText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);

或者在xml中設置:

android:imeOptions="actionSearch"


但是,僅僅設置這些還不夠,需要配合屬性:

android:singleLine="true"
android:inputType="text"

使用,纔有效果。


然後,給這個EditText設置動作監聽,在onEditorAction回調方法中做對應的搜索邏輯

代碼如下:

        mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //TODO
                    return true;
                }
                return false;
            }
        };


發佈了120 篇原創文章 · 獲贊 57 · 訪問量 28萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章