调用下面代码:(第一次调用显示,再次调用则隐藏,如此反复),this指activity
InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); imm.showSoftInput(myview, InputMethodManager.SHOW_IMPLICIT);
来检查虚拟键盘是不是在开启的状态:
if (imm.isActive())
空白处隐藏软键盘:
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
System.out.println("down");
if (RegisterActivity.this.getCurrentFocus() != null) {
if (RegisterActivity.this.getCurrentFocus().getWindowToken() != null) {
imm.hideSoftInputFromWindow(RegisterActivity.this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
return super.onTouchEvent(event);
}
一般情况下,都是在点击某个View的时候,如果键盘没有关闭的时候,自动的去隐藏键盘,因此,在onClick方法中这么写即可:
//隐藏软键盘
InputMethodManager imm = ( InputMethodManager ) v.getContext( ).getSystemService( Context.INPUT_METHOD_SERVICE );
if ( imm.isActive( ) ) {
imm.hideSoftInputFromWindow( v.getApplicationWindowToken( ) , 0 );
}
程序启动后自动弹出软件盘:
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager)this.getSystemService(INPUT_METHOD_SERVICE); imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
Toast.makeText(chick.this, "show", Toast.LENGTH_SHORT).show();
}
}, 1000);