[英]Hide keyboard not working with custom dialog on fragment
I got a custom dialog.我有一个自定义对话框。 The numeric keyboard appears automatically when the dialog appears and edit field gets focus.
当对话框出现并且编辑字段获得焦点时,数字键盘会自动出现。 But when I click on grey space around dialog as itself dialog disappear but keyboard is still open.
但是当我点击对话框周围的灰色空间时,对话框本身消失了,但键盘仍然打开。 I have tried Close/hide android soft keyboard but it's not working.
我试过关闭/隐藏 android 软键盘,但它不起作用。
My code我的代码
private Button mSearchBtn;
private EditText mEditText;
private DialogUserSearchPresenter mPresenter;
public DialogUserSearch(@NonNull Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_search);
mPresenter = new DialogUserSearchPresenter(new MainInteractor(new UserRepository(new Prefs(getContext())), new HotUsersRepository(new Prefs(getContext())), new SystemRepository(new Prefs(getContext()))));
mEditText = (EditText) findViewById(R.id.dialog_search_edit);
mEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
mSearchBtn = (Button) findViewById(R.id.dialog_search_button);
mSearchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mEditText.getText().length() == 10) {
mPresenter.searchUser(mEditText.getText().toString().trim());
closeKeyboard();
} else {
Toast.makeText(getContext(), getContext().getString(R.string.dialog_search_quantity_error),
Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onStop() {
super.onStop();
closeKeyboard();
}
private void closeKeyboard() {
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) getContext().getSystemService(getContext().INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
1)Can you try this: 1)你可以试试这个:
private void closeKeyboard(View view) {
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
call this way:这样调用:
closeKeyboard(mEditText);
I tried it works我试过了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.