2017-04-10 81 views
0

我處於有editText格式的情況。當我在編輯文本中按添加,我在列表中添加一個成員。何時添加(或不)我打開一個自定義對話框。在自定義對話框關閉後隱藏Android中的軟鍵盤

在我的活動我在編輯文本上添加按鈕,點擊時有這樣的代碼:

customDialogTeamMember = new CustomDialogTeamMember(............); 
    customDialogTeamMember.makeDialog(); 
    editText.getText().clear(); 
    editText.clearFocus(); 
    hideSoftKeyboard(); 

我hideSoftKeyboard()就像定義:

public void hideSoftKeyboard() { 
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
} 

這種方法在其他部分工作應用程序。但這裏不行!

打開自定義對話框。當我關上它時,鍵盤會保持在屏幕上。可能是什麼問題呢?!

+1

你有沒有覆蓋onDismissDialog,並調用hideSoftKeyboa RD();在裏面呢? – AlexTa

+1

請參閱: - http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard – Hangman

回答

2

顯示和隱藏鍵盤

InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); 
//Hide: 
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); 
//Show 
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0); 




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

private void showKeyboard() { 
    // Check if no view has focus: 
    View view = this.getCurrentFocus(); 
    if (view != null) { 
     InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); 
     inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); 
    } 
} 
0

我在我的案例中使用了這個。

// for hide keyboard 
    public static void hideKeyboard(Activity activity) { 
     InputMethodManager iim = (InputMethodManager) 
       activity.getSystemService(Context.INPUT_METHOD_SERVICE); 

     if (activity.getCurrentFocus() != null) 
      iim.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 
        InputMethodManager.HIDE_NOT_ALWAYS); 
    } 

將此方法作爲您的requirements.and讓我知道..希望這會有所幫助。

0
// Check if no view has focus: 
View view = this.getCurrentFocus(); 
if (view != null) { 
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
} 

檢查了這一點

0

隱藏和顯示軟鍵盤,用下面的方法

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

private void showKeyboard() { 
    // Check if no view has focus: 
    View view = this.getCurrentFocus(); 
    if (view != null) { 
     InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); 
     inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); 
    } 
} 
0
// minimize keyboard 
    try { 
     InputMethodManager imm = (InputMethodManager)activity.getSystemService(INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(dialog.getCurrentFocus().getWindowToken(), 0); 
    } catch (Exception e) {}