gpt4 book ai didi

android - 在 AlertDialog 中隐藏键盘

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:50:18 25 4
gpt4 key购买 nike

我有一个带有编辑文本的警报对话框。对于这个 Edittext,我让键盘出现,我希望当用户按下 ok 或 cancel 来隐藏键盘时。 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.

这是我的代码:

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();

我的赌注在哪里?谁能帮帮我?

最佳答案

我找到了解决方案:

alert.setNegativeButton(cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
saveimage();
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(input.getWindowToken(), 0);
dialog.cancel();
}
});

我应该在隐藏键盘后放置 dialog.cancel()。

KOTLIN 更新:

val im: InputMethodManager =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
im.hideSoftInputFromWindow(editText.windowToken, 0)

关于android - 在 AlertDialog 中隐藏键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7270508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com