我的布局中有EditText
和Button
。在编辑字段中写入并单击此按钮到go back my fragment
后,我想隐藏虚拟键盘。我认为这很简单,但我尝试了某种方式,但它不起作用:
该代码显示Button
如何工作:
private void onButtonClicked(){
getActivity().getSupportFragmentManager().popBackStack();
}
某些解决方案的代码,但无法提供帮助。
这段代码我使用hideSoftInputFromWindow
但是当我调用' EditText.getWindowToken()'时,它不会隐藏软键盘(我也将0值更改为InputMethodManager.HIDE_IMPLICIT_ONLY或InputMethodManager.HIDE_NOT_ALWAYS和它不行):
EditText myEditText = (EditText) findViewById(R.id.myEditText);
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
使用此代码,在此应用程序的另一个屏幕中,它可以正常工作。这个屏幕是一个活动,所以我认为这个问题是片段问题。
我的片段代码:
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);
}
答案 0 :(得分:7)
在Fragment
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);