Android簡單的動態控制軟鍵盤的顯隱:
隱藏軟鍵盤方法:
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
源碼注釋:
/** Window flag: invert the state of {@link #FLAG_NOT_FOCUSABLE} with
* respect to how this window interacts with the current method. That
* is, if FLAG_NOT_FOCUSABLE is set and this flag is set, then the
* window will behave as if it needs to interact with the input method
* and thus be placed behind/away from it; if FLAG_NOT_FOCUSABLE is
* not set and this flag is set, then the window will behave as if it
* doesn't need to interact with the input method and can be placed
* to use more space and cover the input method.
*/
public static final int FLAG_ALT_FOCUSABLE_IM = 0x00020000;
該方法會導致界面裡面所有需要彈出軟鍵盤的控件均無法顯示軟鍵盤。
當然當我們需要清除該狀態時,可以在輸入控件的Touch事件裡面進行解除
EditText.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
return false;
}
});