Android软键盘的显示和隐藏

//隐藏软键盘
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
/**
 * 显示软件盘
 * 进入页面打开软键盘
 * 首先要对指定的输入框请求焦点。然后调用输入管理器弹出软键盘。
 *警告:对于刚跳到一个新的界面就要弹出软键盘的情况上述代码可能由于界面为加载完全而无法弹出软键盘。
 *此时应该适当的延迟弹出软键盘如200毫秒(保证界面的数据加载完成)。实例代码如下:
 */
private void setEditTextState() {
    et_code.setFocusable(true);
    et_code.setFocusableInTouchMode(true);
    et_code.requestFocus();

    Timer timer = new Timer();
    timer.schedule(new TimerTask()
                   {

                       public void run()
                       {
                           InputMethodManager  inputManager =
                                   (InputMethodManager)et_code.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                           inputManager.showSoftInput(et_code, 0);
                       }

                   },
            200);
}
et_code:表示控件是否选中

原创文章 47 获赞 34 访问量 3万+

猜你喜欢

转载自blog.csdn.net/xige1995/article/details/79865864