在 AlertDialog 中隐藏键盘
我有一个带有编辑文本的警报对话框。对于此 Edittext,我让键盘出现,并且我希望当用户按确定或取消时隐藏键盘。奇怪的问题是,当用户选择“确定”时,键盘会隐藏,但是当选择“取消”时,键盘不会隐藏,我在这两种情况下使用相同的代码。
这是我的代码:
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(data);
final EditText input = new EditText(this);
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(25);
input.setFilters(FilterArray);
input.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(input, 0);
}
},200);
alert.setView(input);
alert.setPositiveButton(ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
text = input.getText().toString().trim();
Canvas c = new Canvas(bitmapResult);
drawTextImage(bitmapResult);
saveimage();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
}
});
alert.setNegativeButton(cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
saveimage();
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(input.getWindowToken(), 0);
}
});
alert.show();
我的赌注在哪里?谁能帮助我吗?
I have an alertdialog with an editext. For this Edittext I make keyboard appear and I want that when user press ok or cancel to hide the keyboard. The strange problem is that when user choose ok, the keyboard is hide, but when choose cancel the keyboard doesn't hide an I'm using the same code for both cases.
Here is my code :
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(data);
final EditText input = new EditText(this);
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(25);
input.setFilters(FilterArray);
input.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(input, 0);
}
},200);
alert.setView(input);
alert.setPositiveButton(ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
text = input.getText().toString().trim();
Canvas c = new Canvas(bitmapResult);
drawTextImage(bitmapResult);
saveimage();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
}
});
alert.setNegativeButton(cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
saveimage();
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(input.getWindowToken(), 0);
}
});
alert.show();
where is my mystake? Can anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我找到了解决方案:
我应该在隐藏键盘后放置dialog.cancel()。
KOTLIN 更新:
I found the solution :
I should've put dialog.cancel() after I hide the keyboard.
UPDATE IN KOTLIN:
我也一直在努力解决这个问题,并且对几乎所有发布的“解决方案”都敲了敲头,但该死的键盘仍然无法关闭。然后我有了一个充满咖啡因的愿景:
请注意 HIDE_IMPLICIT_ONLY
希望可以帮助其他遇到此问题的人。
I too was struggling with this and bonked my head on just about every "solution" which was posted yet the damn keyboard would still not close. Then I had a caffenated vision:
Note the HIDE_IMPLICIT_ONLY
hope that helps anyone else struggling with this problem.
在使用
dialog.cancel();
之前使用以下方法Use following method before you use
dialog.cancel();
就我而言,我希望仅在显示对话框时打开键盘
我尝试了很多解决方案,但最终我通过添加清单文件的内部标签成功实现了
。
In my case i wanted keyboard to be open only when the dialog shown
i have tried many solutions but finally i have succeeded to achieve by adding
inside tag of manifiest file.
不确定,但您可以尝试添加此内容:
我用它来避免在我的应用程序启动时首次显示键盘...当我单击该字段时,键盘仍然打开...
所以,也许,它可以与您的代码一起使用:
然后自动关闭它...
Not sure, but you can try with adding this:
I'm using it to avoid the first display of the keyboard when my app starts... when I click in the field, the keyboard is still opened...
So, maybe, it could work with your code:
and then automatically close it...
尝试了以上所有方法,但最后这对我有用......在失去了一些头发之后 -_-
Tried all of the above but finally this works for me... after a few lost hair -_-