How to hide Android soft keyboard on EditText

前端 未结 16 1112
长发绾君心
长发绾君心 2020-11-27 14:38

I have an Activity with some EditText fields and some buttons as a convenience for what normally would be used to populate those fields. However when we the user touches on

相关标签:
16条回答
  • 2020-11-27 15:11

    This will help you

    editText.setInputType(InputType.TYPE_NULL);
    

    Edit:

    To show soft keyboard, you have to write following code in long key press event of menu button

    editText.setInputType(InputType.TYPE_CLASS_TEXT);
                editText.requestFocus();
                InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                mgr.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
    
    0 讨论(0)
  • 2020-11-27 15:14

    Three ways based on the same simple instruction:

    a). Results as easy as locate (1):

    android:focusableInTouchMode="true"
    

    among the configuration of any precedent element in the layout, example:

    if your whole layout is composed of:

    <ImageView>
    
    <EditTextView>
    
    <EditTextView>
    
    <EditTextView>
    

    then you can write the (1) among ImageView parameters and this will grab android's attention to the ImageView instead of the EditText.

    b). In case you have another precedent element than an ImageView you may need to add (2) to (1) as:

    android:focusable="true"
    

    c). you can also simply create an empty element at the top of your view elements:

    <LinearLayout
      android:focusable="true"
      android:focusableInTouchMode="true"
      android:layout_width="0px"
      android:layout_height="0px" />
    

    This alternative until this point results as the simplest of all I've seen. Hope it helps...

    0 讨论(0)
  • 2020-11-27 15:19

    After long time looking into TextView class I found a way to prevent keyboard to appears. The trick is hide it right after it appears, so I searched a method that is called after keyboard appear and hide it.

    Implemented EditText class

    public class NoImeEditText extends EditText {
    
        public NoImeEditText(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        /**
         * This method is called before keyboard appears when text is selected.
         * So just hide the keyboard
         * @return
         */
        @Override
        public boolean onCheckIsTextEditor() {
            hideKeyboard();
    
            return super.onCheckIsTextEditor();
        }
    
        /**
         * This methdod is called when text selection is changed, so hide keyboard to prevent it to appear
         * @param selStart
         * @param selEnd
         */
        @Override
        protected void onSelectionChanged(int selStart, int selEnd) {
            super.onSelectionChanged(selStart, selEnd);
    
            hideKeyboard();
        }
    
        private void hideKeyboard(){
            InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindowToken(), 0);
        }
    }
    

    and style

    <com.my.app.CustomViews.NoImeEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:editable="false"
        android:background="@null"
        android:textSize="@dimen/cell_text" />
    
    0 讨论(0)
  • 2020-11-27 15:19

    Simply use below method

    private fun hideKeyboard(activity: Activity, editText: EditText) {
        editText.clearFocus()
        (activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(editText.windowToken, 0)
    }
    
    0 讨论(0)
提交回复
热议问题