2016-02-06 23 views
0

我有這個OnFocusChangeListener如何保持鍵盤上的焦點變化開放,但不關閉並重新打開

View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      InputMethodManager myIMM = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
      if(hasFocus) { 
       myIMM.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0); 
      } else { 
       myIMM.hideSoftInputFromWindow(v.getWindowToken(), 0); 
      } 
     } 
    }; 

我這個監聽器設置爲每EditText上我做編程。有效,但有時不。它給出了很多這樣的警告:

W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 

有時我可以看到鍵盤關閉並重新打開。 所以,我認爲這個問題是因爲我打開和關閉鍵盤很多次。我怎樣才能保存鍵盤狀態?所以,如果新焦點是EditText,鍵盤不會關閉。

對不起,英文不好,我希望你能理解。

回答

1

問題是你這樣做的方式,它必然會發生。你正在給兩個三個異步調用。 myImm.toggleSoftInput和hideSoftInput越來越由兩種不同的觀點幾乎在同一時間被稱爲:

  1. 是越來越聚焦和
  2. 被釋放焦點。

現在這個調用通過服務,並不能保證哪個調用會被輸入方法首先接收,因此行爲是隨機的。

現在,如果你希望你的鍵盤打開時,視圖清晰,那麼你只需要做到這一點

InputMethodManager myIMM = 
    (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
     myIMM.showSoftInput(view, 2); 

所有上面的代碼需要,如果你想在視圖上的焦點開的鍵盤,不會自動打開鍵盤,但如果您只使用編輯文本,那麼我認爲您甚至不希望這個監聽器。

如果你需要隱藏鍵盤,我建議做這樣的事情:

  1. 創建一個處理程序,併發送延時請求它。
  2. 刪除舊的消息並保留最新的請求。
private static class SearchHandler extends Handler { 
    private WeakReference<CitySelectionActivity> mTarget; 

    SearchHandler(CitySelectionActivity target) { 
     mTarget = new WeakReference<>(target); 
    } 

    public void setTarget(CitySelectionActivity target) { 
     mTarget.clear(); 
     mTarget = new WeakReference<>(target); 
    } 

    @Override 
    public void handleMessage(final Message msg) { 
     if (msg.what == 0) { 
     InputMethodManager myIMM = (InputMethodManager) mTarget.get().getSystemService(Context.INPUT_METHOD_SERVICE); 
     myIMM.showSoftInput((View)msg.obj,0); 
     } 
     if(msg.what ==1){ 
     InputMethodManager myIMM = (InputMethodManager) mTarget.get().getSystemService(Context.INPUT_METHOD_SERVICE); 
     myIMM.hideSoftInputFromWindow(((View)msg.obj).getWindowToken(),0); 
     } 
    } 
    } 

    View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() { 
    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     Message message = new Message(); 
     message.obj = v; 
     if (hasFocus) { 
     message.what = 0; 
     handler.removeMessages(0); 
     handler.removeMessages(1); 
     handler.sendMessageDelayed(message, 200); 
     } else { 
     message.what = 1; 
     handler.removeMessages(0); 
     handler.removeMessages(1); 
     handler.sendMessageDelayed(message, 200); 
     } 
    } 
    }; 
+0

是的,但我想那鍵盤收盤失去焦點。 –

相關問題