控制软键盘显示和隐藏

控制软键盘显示和隐藏

Android IT:

Android应用开发中有时可能会需要通过代码控制输入法软件盘的显示与隐藏,如下:

 

方法一:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEdit.getWindowToken(), 0); //myEdit是你的EditText对象

 下面就可以通过下面方法可以恢复显示

 boolean  showSoftInput(View view, int flags, ResultReceiver resultReceiver)
 boolean  showSoftInput(View view, int flags)
 void  showSoftInputFromInputMethod(IBinder token, int flags)

 

方法二:

 protected void hideKeyboard() {
        InputMethodManager localInputMethodManager = (InputMethodManager) getSystemService("input_method");

        View currentFocus = getCurrentFocus();

        if (currentFocus != null) {
            IBinder localIBinder = getCurrentFocus().getWindowToken();
            localInputMethodManager.hideSoftInputFromWindow(localIBinder, 0);
        }
    }

你可能感兴趣的:(控制软键盘显示和隐藏)