美文网首页
EditText焦点

EditText焦点

作者: 逐鹿不顾兔208 | 来源:发表于2018-09-17 09:29 被阅读0次

需求

EditText 有时候进入界面就取得焦点,有时候是进入没有取得焦点,但是点击后需要取得焦点。

  • 在EditText的父控件中添加两样属性,与EditText抢焦点。
     android:focusableInTouchMode="true"
     android:focusable="true 

需求

在ViewPager中有4个fragment,第一个fragment中点击后出现弹框DialogFragment,然后弹框中DialogFragment中有4个fragment,其中一个有EditText,取得焦点后弹出软键盘,是去焦点隐藏软键盘。
也即是有3层Fragment嵌套,

  • 问题是:点击弹框外面,调用隐藏软键盘。
    下面是显示软键盘代码:
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方法中调用隐藏软键盘代码:

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

相关文章

网友评论

      本文标题:EditText焦点

      本文链接:https://www.haomeiwen.com/subject/jjahnftx.html