Close/hide the Android Soft Keyboard on activity state onStop
我的布局中有一个
该代码显示了
1 2 3 | private void onButtonClicked(){ getActivity().getSupportFragmentManager().popBackStack(); } |
这是一些解决方案的代码,但这无济于事。此代码i使用
1 2 3 4 | EditText myEditText = (EditText) findViewById(R.id.myEditText); InputMethodManager imm = (InputMethodManager)getSystemService( Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0); |
有了这个代码,在这个应用程序的另一个屏幕上,它就工作了。这个屏幕是一个活动,所以我认为问题是fragment的问题。
我的片段代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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(); } } |
我的活动代码:
1 2 3 4 5 6 7 | @Override public void onStop() { super.onStop(); InputMethodManager imm = (InputMethodManager) getSystemService( Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mEdtUserName.getWindowToken(), 0); } |
在这种方式,
1 2 | InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getView().getWindowToken(), 0); |