1 /** 2 * 隐藏软键盘 3 * @param v 4 */ 5 public static void hideSoftKeyboard(View v) { 6 InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 7 imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 8 }
1 /** 2 * 开启软键盘 3 * @param v
*注:当软盘关闭时自动弹出 4 */ 5 public static void showSoftKeyboard(View v) { 6 InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 7 imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS); 8 }
注意:view的对象
一定要传当前的v
附:
当activity启动时自动弹出软键盘:
1 public void onCreate(Bundle savedInstanceState) { 2 super.onCreate(savedInstanceState); 3 setContentView(R.layout.main); 4 Timer timer = new Timer(); 5 timer.schedule(new TimerTask() { 6 @Override 7 public void run() { 8 //此处EditText获得焦点或者软键盘弹出操作 9 InputMethodManager imm = InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 10 imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS); 11 } 12 }, 1000); //在一秒后操作