1

我使用上下文ActionBar進行編輯,當我有鍵盤顯示,我想通過按下硬件後退按鈕隱藏它,它隱藏鍵盤,但它也取消了上下文操作欄,我真的找不到如何保持它的方式。上下文ActionBar隱藏,當我點擊硬件後退,鍵盤輸出

有人嗎?

+0

以及鍵盤,因爲用戶輸入的東西。他想隱藏鍵盤。所以他點擊後退按鈕。但該動作調用上下文操作欄以取消自己以及... – vandus

+0

不,我打開一個新的片段,當用戶點擊一個項目來編輯項目。在整個過程中,CAB應始終保持可見狀態。就像您在谷歌驅動器中打開文檔時一樣。只有我認爲這是一種常規行爲 - 任何時候當您顯示CAB並且鍵盤已經啓動並且您想要隱藏鍵盤時,您按下後退按鈕,上下文操作欄應該意識到後按是用於鍵盤的,不適合CAB。再次,它在Google雲端硬盤應用中就像這樣。 – vandus

+0

我認爲你沒有達到指導原則。當用戶調用CAB時,通常會搜索當前活動(或片段)中的某些內容。所以要看到他必須隱藏鍵盤的結果。另外,當用戶沒有找到預期的結果時,他會用相同的CAB重新搜索。然後,不,我認爲你沒有超出準則。 – Fllo

回答

1

你應該儘量覆蓋Back Key硬件,並用boolean處理預期的行爲如下:

// boolean isVisible to retrieve the state of the SoftKeyboard 
private boolean isVisible = false; 

// isVisible becomes 'true' if the user clicks on EditText 

// then, if the user press the back key hardware, handle it: 
@Override 
public boolean onKeyPreIme(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     // check isVisible 
     if(isVisible) { 
      // hide the keyboard 
      InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE); 
      mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
      isVisible = false; 
     } else { 
      // remove the CAB 
      mActionMode.finish(); 
     } 
    } 
    return false; 
} 

另一種解決方案可能是調用dispatchKeyEvent方法顯示CAB時仍稱:

@Override 
public boolean dispatchKeyEvent(KeyEvent event) { 
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { 
     // check CAB active and isVisible softkeyboard 
     if(mActionModeIsActive && isVisible) { 
      InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE); 
      mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
      isVisible = false; 
      return true; 
     // Maybe you might do not call the 'else' condition, anyway.. 
     } else { 
      mActionMode.finish(); 
      return true; 
     } 
    } 
    return super.dispatchKeyEvent(event); 
} 

這應該是訣竅,但我沒有測試過。希望這可以幫助。
來源:How to Override android's back key when softkeyboard is open - Prevent to cancel Action Mode by press back button

+0

謝謝,我會研究它。請看看我最近對我原來的問題的評論。我認爲這是一個全球性問題,我認爲我沒有做任何違反準則的事情。 – vandus