编辑文本完成后,软键盘再次打开

时间:2019-06-28 13:17:57

标签: java android

所以我有一个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;
       }
});

那么,为什么直接按下按钮却不能正常工作呢?这对我来说很奇怪。任何人都知道发生了什么事吗? :(

2 个答案:

答案 0 :(得分:1)

点击完成按钮后,调用hideSoftInputFromWindow方法-

InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

答案 1 :(得分:0)

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