简体   繁体   English

将键盘屏幕隐藏在片段中

[英]Hide keyboard screen in fragment

I would like to know how to hide the keyboard screen when switching fragments in an activity. 我想知道在活动中切换片段时如何隐藏键盘屏幕。

I have an activity that launches a fragment. 我有一个启动片段的活动。 This fragment has a TextInputEditText view where I can type in some text, automatically the keyboard shows up when the TextInputEditText view is selected. 这个片段有一个TextInputEditText视图,我可以在其中输入一些文本,当选择TextInputEditText视图时,键盘会自动显示出来。 When I am done typing in the text, I click on a programmed "Button" that takes me to another fragment. 输入完文本后,单击已编程的“按钮”,将我带到另一个片段。 Unfortunately, the keyboard screen is still displayed in this new fragment and I want it to be hidden/disappear. 不幸的是,键盘屏幕仍显示在这个新片段中,我希望它被隐藏/消失。

I have tried the following in onCreateView(..), inside on setOnClickListener button, in the fragment that displays the TextInputEditText view. 我在setCreateClickListener按钮中的onCreateView(..)中,在显示TextInputEditText视图的片段中尝试了以下操作。

Snippet: 片段:

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //send to the backend,when response come back, then insert
                Fragment fragment = null;
                Class fragmentClass = HashHomeFragment.class;

                try {
                    fragment = (Fragment) fragmentClass.newInstance();
                }catch (Exception e){
                    e.printStackTrace();
                }
                FragmentManager fragmentManager = getFragmentManager();
                Bundle bundle = new Bundle();
                bundle.putString("hash_message", "thisstorehaslotsofdiscounts");
                fragment.setArguments(bundle);
                fragmentManager.beginTransaction().replace(R.id.hash_container, fragment, "Home").commit();

                //hide keyboard
                InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

            }
        });

But when android goes to the fragment, the keyboard is still present--the code doesn't work! 但是当android转到片段时,键盘仍然存在-代码不起作用!

Can someone please help me figure out where and what code I need to hide the keyboard when I move from one fragment to another ? 有人可以帮我弄清楚当我从一个片段移到另一个片段时,隐藏键盘的位置和代码吗? ... ...

  //hide keyboard
                InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

FragmentManager fragmentManager = getFragmentManager();
                Bundle bundle = new Bundle();
                bundle.putString("hash_message", "thisstorehaslotsofdiscounts");
                fragment.setArguments(bundle);
                fragmentManager.beginTransaction().replace(R.id.hash_container, fragment, "Home").commit();

Reversing seems to do the trick. 倒车似乎可以解决问题。 I forgot that when I call the very last fragmentManger command, the current fragment is in the process of being destroyed and the current view is not pointing to what I think it is. 我忘记了当我调用最后一个fragmentManger命令时,当前的片段正在被销毁,并且当前的视图没有指向我认为的那样。 It makes much more sense to hide the keyboard with the current view intact, and then switch fragment! 隐藏完好当前视图的键盘,然后切换片段,这更有意义。

Maybe you must clear focus of the EditText before hiding the keyboard. 也许您必须在隐藏键盘之前清除EditText的焦点。 Try this function: 试试这个功能:

private void hideKeyboard(Context context) {
    if (context == null || !(context instanceof Activity) || ((Activity) context).getCurrentFocus() == null) {
        return;
    }
    View view = ((Activity) context).getCurrentFocus();
    if (view != null) {
        view.clearFocus();
        InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputManager != null) {
            inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
}
import android.view.inputmethod.InputMethodManager;

InputMethodManager inputManager = (InputMethodManager)
                              getSystemService(Context.INPUT_METHOD_SERVICE); 

inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                 InputMethodManager.HIDE_NOT_ALWAYS);

I put this right after the onClick(View v) event for all the buttons. 我将其放在所有按钮的onClick(View v)事件之后。

The keyboard hides when you click the button. 单击按钮时,键盘隐藏。 hope this helps... 希望这可以帮助...

public void hideKeyboard() {

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

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

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