How to hide Android soft keyboard on EditText

谁说胖子不能爱 提交于 2019-11-26 21:58:55

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);
Emran Hamza

You need to add the following attribute for the Activity in your AndroidManifest.xml.

<activity
    ...
    android:windowSoftInputMode="stateHidden|adjustResize"
    ...
/>

I sometimes use a bit of a trick to do just that. I put an invisible focus holder somewhere on the top of the layout. It would be e.g. like this

 <EditText android:id="@id/editInvisibleFocusHolder"
          style="@style/InvisibleFocusHolder"/>

with this style

<style name="InvisibleFocusHolder">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">0dp</item>
    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">true</item>
    <item name="android:inputType">none</item>
</style>

and then in onResume I would call

    editInvisibleFocusHolder.setInputType(InputType.TYPE_NULL);
    editInvisibleFocusHolder.requestFocus();

That works nicely for me from 1.6 up to 4.x

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" />

The soft keyboard kept rising even though I set EditorInfo.TYPE_NULL to the view. None of the answers worked for me, except the idea I got from nik431's answer:

editText.setCursorVisible(false);
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);

The following line is exactly what is being looked for. This method has been included with API 21, therefore it works for API 21 and above.

edittext.setShowSoftInputOnFocus(false);

There seems to be quite a variety of ways of preventing the system keyboard from appearing, both programmatically and in xml. However, this is the way that has worked for me while supporting pre API 11 devices.

// prevent system keyboard from appearing
if (android.os.Build.VERSION.SDK_INT >= 11) {
    editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
    editText.setTextIsSelectable(true);
} else {
    editText.setRawInputType(InputType.TYPE_NULL);
    editText.setFocusable(true);
}
David Guo

My test result:

with setInputType:

editText.setInputType(InputType.TYPE_NULL);

the soft keyboard disappears, but the cursor will also disappear.

with setShowSoftInputOnFocus:

editText.setShowSoftInputOnFocus(false)

It works as expected.

Nikhil C George

Let's try to set the below properties in your xml for EditText

android:focusableInTouchMode="true" android:cursorVisible="false".

if you want to hide the softkeypad at launching activity please go through this link

TOLIN THOMAS
weekText = (EditText) layout.findViewById(R.id.weekEditText);
weekText.setInputType(InputType.TYPE_NULL);

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...

Hide the keyboard

editText.setInputType(InputType.TYPE_NULL);

Show Keyboard

etData.setInputType(InputType.TYPE_CLASS_TEXT);
etData.setFocusableInTouchMode(true);

in the parent layout

android:focusable="false"
Nur Gazi

Simply Use EditText.setFocusable(false); in activity

or use in xml

android:focusable="false"

Hello Use this code it is working 100% for me

EditText ee=new EditText(this);
ee.setShowSoftInputOnFocus(false);

*But you want to just hide keyboard on edittext click given below code will work but keyboard icon and upward sign of back-button will create *Given code is like this

        EditText ee=new EditText(this);//create edittext
        final   View.OnTouchListener disable =new View.OnTouchListener() {
        public boolean onTouch (View v, MotionEvent event) {
        v.onTouchEvent(event);
        InputMethodManager img = 
        (InputMethodManager)v.getContext().getSystemService(INPUT_METHOD_SERVICE);
        if (img != null) {
        img.hideSoftInputFromWindow(v.getWindowToken(),0);
        }
        return true;
        }
        };
        //call touch listener
       ee.setOnTouchListener(disable);

Use also it in case of text-changing listener

        EditText ee= new EditText(this);
        Text=new TextWatcher(){

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int 
        after) {
        InputMethodManager img = (InputMethodManager) 
        getSystemService(INPUT_METHOD_SERVICE);
        img.hideSoftInputFromWindow(ee.getWindowToken(), 0);

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        InputMethodManager img = (InputMethodManager) 
        getSystemService(INPUT_METHOD_SERVICE);
        img.hideSoftInputFromWindow(ee.getWindowToken(), 0);

        }
       @Override
        public void afterTextChanged(Editable s) {
        InputMethodManager img = (InputMethodManager) 
        getSystemService(INPUT_METHOD_SERVICE);
        img.hideSoftInputFromWindow(ee.getWindowToken(), 0);

        }
        };
       ee.addTextChangedListener(Text);

Use it also in case of onclick listener

 EditText ee=new EditText(this);
 ee.setOnClickListener(new View.OnClickListener() {
 public void onClick(View v) {

 InputMethodManager img= (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
 img.hideSoftInputFromWindow(ee.getWindowToken(),0);

 }});

All the code will work but for me first code is best

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)
}
   public class NonKeyboardEditText extends AppCompatEditText {

    public NonKeyboardEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onCheckIsTextEditor() {
        return false;
    }
}

and add

NonKeyboardEditText.setTextIsSelectable(true);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!