Android基礎——EditText不彈出軟鍵盤解決辦法

         在使用到EditText的時候,由於焦點衝突等原因,偶爾會遇到軟鍵盤彈不出的情況,嘗試使用以下方法解決一下。

        final EditText editText = (EditText) view.findViewById(R.id.et_food_number);

        editText.selectAll();   //默認選中EditText中的所有內容
        editText.setFocusable(true);   //設置可以獲取焦點
        editText.setFocusableInTouchMode(true);     //觸摸可以獲取焦點? 什麼鬼,我也不清楚……
        editText.requestFocus();  //請求獲取焦點(一般情況下,EditText是默認獲取的)
          下面這步也相當重要,或許就是這個原因搞得
        //彈出鍵盤,默認900毫秒後彈出,避免數據未加載完,鍵盤彈不出來
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
                           public void run() {
                               InputMethodManager inputManager =
                                       (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                               inputManager.showSoftInput(editText, 0);
                           }
                       },
                900);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章