简体   繁体   English

软键盘在片段中隐藏问题

[英]SoftKeyboard Hide issue in Fragment

TASK : Have to hide Soft_Keyboard when user comes back from Recent to Application.任务:当用户从最近返回到应用程序时,必须隐藏 Soft_Keyboard。 (Taking about coming back to Fragment from Recent), There should no Soft_Keyboard in open mode. (考虑从最近回到 Fragment),在打开模式下应该没有 Soft_Keyboard。

ISSUE : Soft_keyboard remains open when i am coming back from recent mode.问题:当我从最近模式返回时,Soft_keyboard 保持打开状态。

EFFORTS : To hide soft_keyboard I have done below lines of Code in onStart(), onResume(), onCreate() and also in my custom method init() inside Fragment.努力:为了隐藏 soft_keyboard,我在 onStart()、onResume()、onCreate() 以及我在 Fragment 中的自定义方法 init() 中完成了以下代码行。

CODE : as below :代码:如下:

  1. CALL : CommonUtil.hideSoftKeyboard(getActivity());调用: CommonUtil.hideSoftKeyboard(getActivity());
  2. Lines :线路:

     public static void hideSoftKeyboard(Context context) { try { InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE); View focusedView = ((Activity) context).getCurrentFocus(); //If no view is focused, an NPE will be thrown if (focusedView != null) { inputMethodManager.hideSoftInputFromWindow(((Activity) context).getCurrentFocus().getWindowToken(), 0); } } catch (Exception e) { e.printStackTrace(); } }

.. I have tried above code in [enter code] but, it still looks like this: hope you can understood. .. 我已经在[输入代码]中尝试了上面的代码,但它仍然是这样的:希望你能理解。

Anyway,反正,

What might be the solution ?可能的解决方案是什么? Thanks.谢谢。

NOTE : I don't wanna to use ADJUST_PAN .注意:我不想使用ADJUST_PAN ;) ;)

I'm also getting same issue when try to close the soft-keyboard from Fragment but I solved with passing the view which is having the focus and below code works fine for me.尝试从Fragment关闭软键盘时,我也遇到了同样的问题,但我通过传递具有焦点的视图解决了,下面的代码对我来说很好用。

  public void hideKeyboard(View view) {
    if (view != null) {
        InputMethodManager imm = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

When the fragment is no longer visible, the EditText View still retains the focus so the keyboard remains visible.当片段不再可见时,EditText 视图仍然保持焦点,因此键盘保持可见。 However;然而; When the fragment transitions from visible to no longer visible execution can be intercepted as follows:当片段从可见过渡到不再可见时,可以按如下方式拦截执行:

... ...

override fun onStop() {
// Fragment transitions from visible to not visible
super.onStop()
hideSoftKeyboard()}
    
fun hideSoftKeyboard(){
myEditTextView1.clearFocus()
myEditTextView2.clearFocus()
//etc
}
    

... ...

If you cannot be sure which Edit textview has the focus you must clearFocus() on all views as you move from one fragment to another.如果您不能确定哪个编辑文本视图具有焦点,则当您从一个片段移动到另一个片段时,您必须在所有视图上 clearFocus()。 This worked for me like a charm....这对我来说就像一个魅力......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM