所以我有一个custom alert dialog和一个EditText。每当我点击一个按钮来确认,或者如果我点击软键盘的自己的完成按钮,我已经编程应用程序关闭对话框。但是,由于一些奇怪的原因,警报对话框关闭后,soft-keyboard仍处于打开状态.
buttonConfirm末尾的这段代码就是我试图解决这个问题的地方。由于某些原因,该代码不适用于按钮本身,但该代码适用于软键盘中的完成按钮。
buttonConfirm.setOnClickListener(new
View.OnClickListener()
{..............
.................
closeKeyboard();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
dialog.dismiss();
}
}, 100); // 5000ms delay
}
//This is the code for the done-button in the `soft keyboard`
textinputEdit.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event){
if(actionId==EditorInfo.IME_ACTION_DONE){
buttonConfirm.performClick();
}
return false;
}
});那么,为什么在直接按下按钮的时候,这个按钮就不能工作呢?这对我来说很奇怪..。有人知道这是怎么回事吗?
发布于 2019-06-28 13:26:03
单击“完成”按钮时,调用hideSoftInputFromWindow方法-
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);发布于 2019-06-28 13:25:00
public void hideSoftKeyboard(Context context, View view) {
try {
InputMethodManager inputMethodManager =
(InputMethodManager) context.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
view.getWindowToken(), 0);
} catch (Exception e) {
e.printStackTrace();
}
}用法
textinputEdit.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
buttonConfirm.performClick();
hideSoftKeyboard(YourActivityName.this,textinputEdit);
}
return false;
}
});https://stackoverflow.com/questions/56807691
复制相似问题