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); //在一秒後操作