简体   繁体   English

我无法在Android上隐藏虚拟键盘

[英]I can't hide the virtual keyboard on Android

I am working with fragments and nesting fragments within fragments using the support library. 我正在使用支持库处理碎片并在片段中嵌套片段。

I have a scenario where I add a new fragment (which contains an EditText) from within the existing fragment. 我有一个场景,我在现有片段中添加一个新片段(包含一个EditText)。 When the user taps on the EditText a virtual keyboard is shown. 当用户点击EditText时,会显示虚拟键盘。 But while the keyboard is open the user can press the home button from the ActionBar which removes the fragment from the stack, but the keyboard still remains open. 但是当键盘打开时,用户可以按下ActionBar中的主页按钮,从堆栈中删除片段,但键盘仍然保持打开状态。 I can not force a close on the keyboard, I tried all code snippets. 我无法强行关闭键盘,我尝试了所有代码片段。 Given the described scenario, can anyone guide me as to how can I solve this ? 鉴于所描述的情景,任何人都可以指导我如何解决这个问题?

EDIT: I made a callback function which I call from the fragments onDestroy. 编辑:我做了一个回调函数,我从片段onDestroy调用。 The MainActivity which hosts all fragments implements this callback: 承载所有片段的MainActivity实现此回调:

@Override
public void onHideSoftKeyboard(EditText editText) {
    // HIDE SOFT KEYBOARD HERE 

final InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

     Toast.makeText(this,"KEYBOARD HIDDEN",Toast.LENGTH_LONG).show();
}

I get the Toast message and the fragment is destroyed on the back button (ActionBar back button), only the keyboard is still present. 我收到Toast消息,并且在后退按钮(ActionBar后退按钮)上销毁了片段,只有键盘仍然存在。

@Override
public void onDestroy() {
    hideSoftKeyboard.onHideSoftKeyboard(editTextComment);



    super.onDestroy();
}

Try to force the keyboard with this: 尝试用这个强制键盘:

InputMethodManager imm = (InputMethodManager)getSystemService(
  Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);  

You also can like this: 你也可以这样:

imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);  

If you want to hide when the user click on Up Home Button, try like this in your onOptionsItemSelected method: 如果要在用户单击“上一页”按钮时隐藏,请在onOptionsItemSelected方法中尝试这样:

case android.R.id.home:  
     // count the active fragment
     if(getSupportFragmentManager().getStackBackEntryCount() > 0) {
         // hide soft method as above
         InputMethodManager mImm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); 
         mImm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
         // do the pop backstack
         getSupportFragmentManager().popBackStack(); 
     } else {  
         // some stuff like finish the activity
     }
     return true;
// other items...

You can do the same with the back button when you use the (override) onBackPressed method. 使用(覆盖)onBackPressed方法时,可以使用后退按钮执行相同操作。

You can use following code. 您可以使用以下代码。

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
}

I've fixed this issue with the following. 我用以下方法解决了这个问题。 First, if you want to pop the keyboard automatically when the activity launches, write the code below in the onCreate method. 首先,如果要在活动启动时自动弹出键盘,请在onCreate方法中编写以下代码。

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

Then, if you want to close the keyboard, use the following. 然后,如果要关闭键盘,请使用以下命令。

InputMethodManager imm = (InputMethodManager)  getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY,0);

I solved this issue with the next solution 我用下一个解决方案解决了这个问题

You need extend every your Fragment from BaseFragment as below: 您需要从BaseFragment扩展每个Fragment,如下所示:

public class BaseFragment extends Fragment {

    @Override
    public void onDestroyView() {
        hideKeyboard(getView());
        super.onDestroyView();
    }

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

And all fragments that you want to be hide the keyboard when it will closed, must extend BaseFragment: 并且在关闭时要隐藏键盘的所有片段必须扩展BaseFragment:

public class EditTextFragment extends BaseFragment {
...
}

As a bonus in every extended fragment you can use hideKeyboard(View view) method to hide keyboard when you want in any place in your fragment 作为每个扩展片段的奖励,您可以使用hideKeyboard(View view)方法在片段中的任何位置隐藏键盘

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

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