2013-03-14 82 views
27

當我們有一個EditText,它失去了焦點(對於不需要鍵盤的元素),軟鍵盤應該自動隱藏還是我們應該自己隱藏它?在失去焦點時隱藏軟鍵盤

我移動從AutoCompleteSearchView對焦(應該表現得像一個EditText我猜的)到ButtonrequestFocus()返回true,但鍵盤不隱藏。

+0

它應該自動隱藏。你甚至嘗試過這樣做嗎? – 2013-03-14 15:08:33

+4

我有,這就是爲什麼我問。 – FWeigl 2013-03-14 15:09:02

+0

@Ascorbin不要搞砸了android會照顧這個.. – Pragnani 2013-03-14 15:12:13

回答

53

最好的辦法找到更多的信息來設置的EditText一個OnFocusChangeListener,然後將代碼添加到鍵盤到聽者的OnFocusChange方法。當EditText失去焦點時,Android會自動關閉鍵盤。

像這樣的事情在你onCreate方法:

EditText editText = (EditText) findViewById(R.id.textbox); 
OnFocusChangeListener ofcListener = new MyFocusChangeListener(); 
editText.setOnFocusChangeListener(ofcListener); 

然後添加類:

private class MyFocusChangeListener implements OnFocusChangeListener { 

    public void onFocusChange(View v, boolean hasFocus){ 

     if(v.getId() == R.id.textbox && !hasFocus) { 

      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 

     } 
    } 
} 
+10

然後必須爲應用程序上的每個editText組件完成?是否有任何全球性的設置來定義這種行爲?如果沒有,應用程序中有許多editTexts,我建議擴展editText類,將偵聽器添加到擴展類中,並沿應用程序 – GyRo 2015-02-02 11:42:17

+4

使用修改後的類如何自動創建它?如果我有150到25個表格,我不想每次都這麼做......這個特性應該很明顯。 – Loenix 2016-05-26 08:20:26

4

Android不會爲您隱藏鍵盤。如果你想在鍵盤隱藏在你的EditText失去焦點,請嘗試使用這樣的方法對事件:

private void hideKeypad() { 
    EditText edtView = (EditText) findViewById(R.id.e_id); 

    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(edtView.getWindowToken(), 0); 
} 
+1

優秀的答案非常感謝 – 2017-06-18 10:01:43

2

試試這個,可能會解決您的問題。

private void hideKeyboard() { 
    InputMethodManager mImMan = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
    mImMan.hideSoftInputFromWindow(mYourEdttxtName.getWindowToken(), 0); 
} 

您可以從here.

5

試試這個

/** 
* Hide keyboard on touch of UI 
*/ 
public void hideKeyboard(View view) { 

    if (view instanceof ViewGroup) { 

     for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { 

      View innerView = ((ViewGroup) view).getChildAt(i); 

      hideKeyboard(innerView); 
     } 
    } 
    if (!(view instanceof EditText)) { 

     view.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       hideSoftKeyboard(v); 
       return false; 
      } 

     }); 
    } 

} 

/** 
* Hide keyboard while focus is moved 
*/ 
public void hideSoftKeyboard(View view) { 
    if (view != null) { 
     InputMethodManager inputManager = (InputMethodManager) contentsContext_ 
       .getSystemService(Context.INPUT_METHOD_SERVICE); 
     if (inputManager != null) { 
      if (android.os.Build.VERSION.SDK_INT < 11) { 
       inputManager.hideSoftInputFromWindow(view.getWindowToken(), 
         0); 
      } else { 
       if (this.getCurrentFocus() != null) { 
        inputManager.hideSoftInputFromWindow(this 
          .getCurrentFocus().getWindowToken(), 
          InputMethodManager.HIDE_NOT_ALWAYS); 
       } 
       view.clearFocus(); 
      } 
      view.clearFocus(); 
     } 
    } 
} 
0

可以重寫dispatchTouchEvent方法來實現它:

@Override 
public boolean dispatchTouchEvent(MotionEvent event) { 

    if (event.getAction() == MotionEvent.ACTION_DOWN) { 

     /** 
     * It gets into the above IF-BLOCK if anywhere the screen is touched. 
     */ 

     View v = getCurrentFocus(); 
     if (v instanceof EditText) { 


      /** 
      * Now, it gets into the above IF-BLOCK if an EditText is already in focus, and you tap somewhere else 
      * to take the focus away from that particular EditText. It could have 2 cases after tapping: 
      * 1. No EditText has focus 
      * 2. Focus is just shifted to the other EditText 
      */ 

      Rect outRect = new Rect(); 
      v.getGlobalVisibleRect(outRect); 
      if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) { 
       v.clearFocus(); 
       InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 
      } 
     } 
    } 
    return super.dispatchTouchEvent(event); 
} 

獎勵:在一個EditText獲得焦點的情況下,事件的順序觸發是:

  1. 其他的EditText的onFocusChange()被稱爲(如果其他的EditText失去焦點)
  2. ACTION_DOWN是稱爲
  3. 最後,將調用該EditText的onFocusChange()方法。
0

我的問題與此代碼(片段)解決

LinearLayout linearLayoutApply=(LinearLayout)rootView.findViewById(id.LinearLayoutApply); 

    linearLayoutApply.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if(hasFocus) 
      { 
       hideKeyBoard(v); 
      } 
     } 
    }); 

hideKeyBoard

public void hideKeyBoard(View v) 
{ 
    InputMethodManager imm=(InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0); 
} 
0

這個問題的解決已經發現here
它在活動上使用DispatchTouchEvent,並且不會將每個EditText掛鉤到FocusChange或Touch事件。
這是更好的解決方案。

我Xamarin實現如下:

public override bool DispatchTouchEvent(MotionEvent ev) 
    { 
     if (ev.Action == MotionEventActions.Down) 
     { 
      var text = CurrentFocus as EditText; 
      if (text != null) 
      { 
       var outRect = new Rect(); 
       text.GetGlobalVisibleRect(outRect); 
       if (outRect.Contains((int) ev.RawX, (int) ev.RawY)) return base.DispatchTouchEvent(ev); 
       text.ClearFocus(); 
       HideSoftKeyboard(); 
      } 
     } 
     return base.DispatchTouchEvent(ev); 
    } 

protected void HideSoftKeyboard() 
    { 
     var inputMethodManager = (InputMethodManager) GetSystemService(InputMethodService); 
     inputMethodManager.HideSoftInputFromWindow(CurrentFocus.WindowToken, 0); 
    } 
1

只需創建一個靜態方法

public static void touchScreenAndHideKeyboardOnFocus(View view, final Activity activity) { 

    if (view instanceof EditText) { 
     view.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       if (!hasFocus) { 
        if(activity != null) { 
         InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); 
         if (activity.getCurrentFocus() != null) { 
          inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
         } 
        } 
       } 
      } 
     }); 
    } 

    if (view instanceof ViewGroup) { 
     for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { 
      View innerView = ((ViewGroup) view).getChildAt(i); 
      touchScreenAndHideKeyboardOnFocus(innerView, activity); 
     } 
    } 
} 

的看法是你的佈局的根視圖..但要小心,如果你有另一個焦點聽衆在您的編輯文本..

+0

什麼是「強制」? – Hatim 2016-11-07 21:27:19

+0

@Hatim upss對不起..它是我原來的方法.. – 2016-11-29 14:20:29

相關問題