公司的項目中要求在點擊搜索的時候彈出一個搜索框,搜索框中有一個EditText,用於數據搜索關鍵字,要求在彈出PopupWindow的時候自動彈出軟鍵盤,原以為只要寫上着兩行代碼可以搞的問題:
1 InputMethodManager inputMethodManager=(InputMethodManager) et_search_key.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 2 inputMethodManager.showSoftInput(et_search_key, 0);
在運行調試的時候,問題來了,沒有按照設想的那樣,顯示軟鍵盤。接下來就是找資料,試了好多方法,就是不行,最后使用Handler異步成功了。下面貼出代碼:
1 private void showSoft(){ 2 Handler handle=new Handler(); 3 handle.postDelayed(new Runnable() { 4 5 @Override 6 public void run() { 7 InputMethodManager inputMethodManager=(InputMethodManager) et_search_key.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 8 inputMethodManager.showSoftInput(et_search_key, 0); 9 } 10 }, 0); 11 }