Close/hide the Android Soft Keyboard on activity state onStop

I have an EditText and a Button in my layout. After writing in the edit field and click this button to go back my fragment , I want to hide the virtual keyboard. I assume that there's a simple, but i tried some way and it not work:

That code show how the Button work:

private void onButtonClicked(){
    getActivity().getSupportFragmentManager().popBackStack();
}

That code for some solution but that can't help. This code i using hideSoftInputFromWindow but when i call 'EditText.getWindowToken()', it not hide the soft keyboard (I also change the 0 value to InputMethodManager.HIDE_IMPLICIT_ONLY or InputMethodManager.HIDE_NOT_ALWAYS and it not work):

EditText myEditText = (EditText) findViewById(R.id.myEditText);  
InputMethodManager imm = (InputMethodManager)getSystemService(
      Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

With this code, in another screen of this app, it work. This screen is an activity so i think that problem is fragment's problem.

My fragment code:

public class ChangeEmailFragment extends BaseFragment {
    private TextView mTxtCurrentEmail;
    private EditText mEdtNewEmail;
    private EditText mEdtPassword;
    private TextView mTxtSubmit;

    @Override
    public void onStop() {
        super.onStop();
        if (progressDialog != null && progressDialog.isShowing())
            progressDialog.dismiss();
        if (dialog != null && dialog.isShowing())
            dialog.dismiss();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_change_email, container,
                false);
        mTxtCurrentEmail = (TextView) view.findViewById(R.id.current_email);
        mEdtNewEmail = (EditText) view.findViewById(R.id.edit_email);
        mEdtPassword = (EditText) view.findViewById(R.id.edit_password);
        mTxtSubmit = (TextView) view.findViewById(R.id.button_submmit);
        return view;
    }

    private void showErrorDialog(String msg) {
        Builder builder = new Builder(getActivity());
        builder.setTitle(getString(R.string.fg_change_email_dialog_error_title));
        builder.setMessage(msg);
        builder.setNegativeButton(getText(R.string.common_ok), null);
        dialog = builder.create();
        dialog.show();
    }
}

My activity code:

@Override
    public void onStop() {
        super.onStop();
        InputMethodManager imm = (InputMethodManager) getSystemService(
              Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mEdtUserName.getWindowToken(), 0);
    }

Fragment尝试这种方式

 InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
 imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
链接地址: http://www.djcxy.com/p/2724.html

上一篇: 用Kotlin关闭/隐藏Android软键盘

下一篇: 在活动状态onStop上关闭/隐藏Android软键盘