2016-09-28 76 views
2

我正在使用android studio。我在我的片段頁面中編輯文本,現在我想在單擊EditText外部後隱藏鍵盤。我使用下面的代碼,但它不工作。在碎片中隱藏EditText的鍵盤

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
super.onActivityCreated(savedInstanceState); 
final InputMethodManager imm = (InputMethodManager) 
getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.hideSoftInputFromWindow(locationEt.getWindowToken(), 0); 
} 

由於提前

+0

請儘量將外面或其他視圖點擊事件

public void hideKeyboard(View view) { InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } 

像這樣一個http://stackoverflow.com/questions/4165414/how-to-hide-soft-keyboard-on-android-after-clicking-outside-edittext –

回答

1

嘗試設置一個onFocusChangeListenerEditText。在onFocusChange方法中,你可以像這樣隱藏鍵盤:

mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View view, boolean hasFocus) { 
      InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(locationEt.getWindowToken(), 0); 
     } 
    }); 
+0

謝謝@jennymo。它工作 – raji

+0

很高興我能夠幫助你;-) – jennymo

1

試試這個在下面的函數中傳遞activity。有用。

public static void hideKeyboard(Activity activity) { 
      // Check if no view has focus: 
      View view = activity.getCurrentFocus(); 
      if (view != null) { 
       InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); 
       inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
      } 
     } 
0

請點擊你的父母佈局中使用下面的代碼:

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

寫這個代碼到你的Activity您的片段放置。

@Override 
public boolean dispatchTouchEvent(MotionEvent event) { 
    View view = getCurrentFocus(); 
    boolean ret = super.dispatchTouchEvent(event); 

    if (view instanceof EditText) { 
      try { 
       View w = getCurrentFocus(); 
       int scrcords[] = new int[2]; 
       w.getLocationOnScreen(scrcords); 
       float x = event.getRawX() + w.getLeft() - scrcords[0]; 
       float y = event.getRawY() + w.getTop() - scrcords[1]; 

       if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom())) { 
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
        if (getWindow() != null && getWindow().getCurrentFocus() != null) { 
         imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0); 
        } 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 

    return ret; 
} 

快樂編碼!

0

試試這個它的活動以及片段

public void setupUI(View view) { 
    if (!(view instanceof EditText)) { 
     view.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       //code of hide soft keyboard 
       return false; 
      } 
     }); 
    } 
    if (view instanceof ViewGroup) { 
     for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { 
      View innerView = ((ViewGroup) view).getChildAt(i); 
      setupUI(innerView); 
     } 
    } 
} 
1

使用這種方法工作時

txtHeader.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     hideKeyboard(txtHeader); 
    }});