How to hide Android soft keyboard on EditText

2019-01-06 12:37发布

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 one of the EditText fields the Android soft keyboard automatically appears. I want it to remain hidden by default, unless the user long presses the menu button. I have search for a solution to this and found several answers, but so far I can't get them to work.

I have tried the following:

1 - In the onCreate method,

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

2 - Also in the onCreate method,

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);

3 - and fIn the Manifest file,

<activity android:name=".activityName" android:windowSoftInputMode="stateAlwaysHidden"/>

None of these methods work. Whenever the user clicks on the EditText field, the soft keyboard appears. I only want the soft keyboard to appear if the user explicitly shows it by long pressing the menu key.

Why isn't this working?

13条回答
走好不送
2楼-- · 2019-01-06 12:45

My test result:

with: editText.setInputType(InputType.TYPE_NULL);

the softkeyboard disappear, but the cursor will also disappear.

with: editText.setShowSoftInputOnFocus(false)

It works as expected.

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-06 12:46

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);
查看更多
Bombasti
4楼-- · 2019-01-06 12:49
weekText = (EditText) layout.findViewById(R.id.weekEditText);
weekText.setInputType(InputType.TYPE_NULL);
查看更多
在下西门庆
5楼-- · 2019-01-06 12:49

Simply Use EditText.setFocusable(false); in activity

or use in xml

android:focusable="false"
查看更多
Fickle 薄情
6楼-- · 2019-01-06 12:53

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

查看更多
在下西门庆
7楼-- · 2019-01-06 12:53

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

查看更多
登录 后发表回答