EditText 完成后软键盘再次打开

所以我有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;

       }

});

那么,为什么在直接按下按钮时它会起作用而不是按钮本身呢?这对我来说很奇怪.. 任何人都知道这到底是怎么回事?:(


千万里不及你
浏览 110回答 2
2回答

冉冉说

单击完成按钮时,调用hideSoftInputFromWindow方法 -InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 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;                }            });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java