2012-06-13 32 views
8

我看到this question集合在SearchView EditText上,當我從ActionBar激活搜索時。然而,鍵盤在獲得焦點時不會彈出。不應該,因爲它只是一個正常的EditText? (這是一個正常的EditText?)這種行爲是在Android SDK級別11上看到的。(Samsung Galax Tab 7.7 with stock Android。)使用SearchView在搜索操作欄上激活時顯示軟鍵盤

我有一個解決方法,此刻掛鉤了我的Activity的onOptionsItemSelected(MenuItem item)方法,顯示鍵盤。

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     boolean menuSelectionHandeled = super.onOptionsItemSelected(item); 
     // menu_search is the id of the menu item in the ActionBar 
     if (item.getItemId() == R.id.menu_search) { 
       mInputManager.showSoftInput(null, InputMethodManager.SHOW_IMPLICIT); 
     } 
     return menuSelectionHandeled; 
    } 

其中mInputManagerInputMethodManager的實例。

ActionBar是用ActionBarSherlock構建的,而且由於目標設備是Android 3.x,這可能是導致症狀的原因嗎?按照ActionBarSherlock的FAQ

在Android 3.x的操作欄(又稱蜂窩)不 實現所有的一個在Android的4.x版(冰淇淋三明治 )的功能。爲了在所有平臺 上提供完整的操作欄API,並統一所有Android版本的樣式,使用自定義 實現。

+0

不知道這真的是問題,但我有一個問題,當我的XML文件中包含了'EditText'在從ActionBarSherlock(即示例文件,我不能不要讓我的回調textchange等工作),但是當我改變了一個'SearchView',而是添加了適當的回調它剛剛開始按預期工作。也許給一個嘗試? – lfxgroove

+0

另外,檢查出http://stackoverflow.com/questions/11011091/how-can-i-focus-on-a-collapsible-action-view-edittext-item-in-the-action-bar-wh – lfxgroove

+0

謝謝@Anton這個問題讓我覺得沒有辦法自動顯示鍵盤。他和我有同樣的解決方法。在我的XML中,我絕對使用SearchView。謝謝 – Diederik

回答

0

這應該工作:

SearchView searchView = new SearchView(getContext()); 
searchView.setInputType(InputType.TYPE_CLASS_TEXT); 
searchView.setBackgroundColor(Color.WHITE); 
相關問題