【Android】Keyboardの表示/非表示

Androidで開発をしていると、特定の画面を開いたときにテキスト入力にフォーカスを当て、 Keyboardを表示したい場合があると思います。

その場合、View#requestFocus()を行い、InputMethodManagerでKeyboardを表示/非表示します。

public void showSoftInput(final EditText view){
    if(null == view){
        return;
    }

    view.post(new Runnable() {
        @Override
        public void run() {
            if(null == view){
                return;
            }

            view.requestFocus();
            InputMethodManager input = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if(null != input) {
                input.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
            }
        }
    });
}
public void hideSoftInput(View view){
    if(null == view){
        return;
    }

    view.requestFocus();

    InputMethodManager input = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    if(null != input){
        input.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

参考