2011-08-11 212 views
0

當軟鍵盤上的一鍵按下時,我想隱藏軟鍵盤。 審覈後,我發現唯一途徑某些事件(例如,當使用上編輯文本)隱藏android軟鍵盤當軟鍵盤按一鍵時

+0

你可以使用:(http://developer.android.com/reference/android/視圖/ KeyEvent.html#ACTION_DOWN) – Hanry

回答

1

喜這裏是詳細

要顯示軟鍵盤後softkeybord隱藏

EditText editText = (EditText) findViewById(R.id.myEdit); 
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
// only will trigger it if no physical keyboard is open 
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); 

要隱藏軟鍵盤

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
    mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0); 

和壓倒一切的onKeydownEvent隱藏軟鍵盤

@Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
    //hide the soft keyboard 
    return super.onKeyDown(keyCode, event); 

}

0

這應該工作:

public class KeyBoard { 

    public static void toggle(Activity activity){ 
     InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); 
     if (imm.isActive()){ 
      imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); // hide 
     } else { 
      imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY); // show 
     } 
    }//end method 
}//end class 

KeyBoard.toggle(activity);