在活动状态onStop上关闭/隐藏Android软键盘 [英] Close/hide the Android Soft Keyboard on activity state onStop
问题描述
我的布局中有一个EditText
和Button
.在编辑字段中写入并单击此按钮以go back my fragment
后,我要隐藏虚拟键盘.我认为这里有一个简单的方法,但是我尝试了一些方法,但它不起作用:
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:
该代码显示了Button
的工作方式:
That code show how the Button
work:
private void onButtonClicked(){
getActivity().getSupportFragmentManager().popBackStack();
}
用于某些解决方案的代码,但无济于事.
我使用hideSoftInputFromWindow
编写此代码,但是当我调用'EditText.getWindowToken()'时,它不会隐藏软键盘(我还将0值更改为InputMethodManager.HIDE_IMPLICIT_ONLY或InputMethodManager.HIDE_NOT_ALWAYS,但它不起作用):
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.
我的片段代码:
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);
这篇关于在活动状态onStop上关闭/隐藏Android软键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!