Hiding the android keyboard for EditText

℡╲_俬逩灬. 提交于 2019-11-30 07:58:08

问题


Whenever I click in the EditText the Android keyboard popup window appears, but I don't want the keyboard to pop up.

I want to permanently hide the android keyboard popup for my current application.

How can I do this?


回答1:


Setting the flag textIsSelectable to true disables the soft keyboard.

You can set it in your xml layout like this:

<EditText
    android:id="@+id/editText"
    ...
    android:textIsSelectable="true"/>

Or programmatically, like this:

EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);

The cursor will still be present, you'll be able to select/copy/cut/paste but the soft keyboard will never show.




回答2:


You may try to fake your EditText with a Button like this:

 <Button
     android:id="@+id/edit_birthday"
     style="@android:style/Widget.EditText"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:hint="@string/hint_birthday"/>



回答3:


In the manifest:

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



回答4:


Solution can be found here :

public void onCreate(Bundle savedInstanceState) {

edittext = (EditText) findViewById(R.id.EditText01);

edittext.setOnEditorActionListener(new OnEditorActionListener() {
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
         if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
            InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            in.hideSoftInputFromWindow(edittext.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
         }
         return false;
      }
   });
}



回答5:


Works for all Android versions.

In your XML use the property android:textIsSelectable="true"

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    .
    .
    .
    android:textIsSelectable="true"/>

And, in your Java class:

editText1.setShowSoftInputOnFocus(false);

In Kotlin:

editText1.showSoftInputOnFocus = false



回答6:


These two line should do what you want:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);



回答7:


Try This

 public void disableSoftKeyboard(final EditText v) {
            if (Build.VERSION.SDK_INT >= 11) {
                v.setRawInputType(InputType.TYPE_CLASS_TEXT);
                v.setTextIsSelectable(true);
            } else {
                v.setRawInputType(InputType.TYPE_NULL);
                v.setFocusable(true);
            }
        }



回答8:


Try to add this in yout onCreate() method.

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

Not tested but It Should work!!




回答9:


In your xml file you can use this:

android:editable="false"


来源:https://stackoverflow.com/questions/4841476/hiding-the-android-keyboard-for-edittext

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