Android: Hiding the keyboard in an overridden “Done” keypress of EditText

前端 未结 4 467
一向
一向 2020-12-30 06:17

I have used a bit of Android code to override the \"Done\" button in my EditText field:

   myEditField.setOnEditorActionListener(new TextView.OnEditorActionL         


        
相关标签:
4条回答
  • 2020-12-30 06:51

    Why not:

    myEditField.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
        @Override 
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
            if (actionId == EditorInfo.IME_ACTION_DONE) { 
    
                mySubroutine(); 
            } 
            return false; 
        } 
    }); 
    

    Just return false after you handle your code. This can be interpreted as no matter what your code (mySubroutine()) does it will still use the default action afterwards. If you return "true" you are telling that you are a happy coder and everything that needed to be done has happen in your mySubroutine() and the default action do not need to take action.

    0 讨论(0)
  • 2020-12-30 06:52

    I had the same problem. Immediately after editText VISIBILITY change from GONE to VISIBLE, I had to set the focus and display the soft keyboard. I achieved this using the following code:

            (new Handler()).postDelayed(new Runnable() {
    
            public void run() {              yourEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
                yourEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));                       
    
            }
        }, 200);
    
    0 讨论(0)
  • 2020-12-30 06:56

    You must attach an onClickListener to the button that executes the following code:

    InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(editview.getWindowToken(), 0);
    
    0 讨论(0)
  • 2020-12-30 07:06

    You can close the keyboard by doing:

    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getWindowToken(), 0);
    
    0 讨论(0)
提交回复
热议问题