EditText焦點

需求

EditText 有時候進入界面就取得焦點,有時候是進入沒有取得焦點,但是點擊后需要取得焦點。

  • 在EditText的父控件中添加兩樣屬性,與EditText搶焦點。
     android:focusableInTouchMode="true"
     android:focusable="true 

需求

在ViewPager中有4個fragment,第一個fragment中點擊后出現(xiàn)彈框DialogFragment,然后彈框中DialogFragment中有4個fragment,其中一個有EditText,取得焦點后彈出軟鍵盤,是去焦點隱藏軟鍵盤。
也即是有3層Fragment嵌套,

  • 問題是:點擊彈框外面,調(diào)用隱藏軟鍵盤。
    下面是顯示軟鍵盤代碼:
private void showInput() {
        try {
            InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
            Log.e(TAG, "showInput: exception: " + e.toString());
        }
    }

這是EditText是去焦點,隱藏軟件盤代碼:

 private void hideInput(EditText view) {
        try {
            InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        } catch (Exception e) {
            Log.e(TAG, "hideInput: Exception:" + e.toString());
        }
    }

這是是去在OnDestroyView方法中調(diào)用隱藏軟鍵盤代碼:

 private void destroyInput() {
       try {
           InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
           View               v                  = getActivity().getCurrentFocus();
           inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
       } catch (Exception e) {
           Log.e(TAG, "hideInput: Exception:" + e.toString());
       }
   }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容