需求
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());
}
}