关闭/隐藏活动状态下的Android软键盘



我有一个EditTextButton在我的布局。在编辑字段写入后,点击这个按钮到go back my fragment,我想隐藏虚拟键盘。我假设有一个简单的,但我尝试了一些方法,它不工作:

这段代码显示了Button是如何工作的:

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

这段代码为一些解决方案,但没有帮助。这段代码使用hideSoftInputFromWindow,但当我调用'EditText.getWindowToken()'时,它不会隐藏软键盘(我还将0值更改为InputMethodManager)。HIDE_IMPLICIT_ONLY或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);
有了这段代码,在这个应用程序的另一个屏幕上,它工作了。这个屏幕是一个活动,所以我认为问题是fragment的问题。

我的片段代码:

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

我的活动代码:

@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);

相关内容

  • 没有找到相关文章

最新更新