Android Hide Soft Keyboard from EditText while not losing cursor

前端 未结 14 1906
日久生厌
日久生厌 2020-12-03 04:55

I\'ve come about as far as this which gets me halfway there, but not quite. I have a dialer Fragment that has all the usual Buttons to enter a numb

相关标签:
14条回答
  • 2020-12-03 05:33

    If your min SDK is 21, you can this method from java code:

    editText.setShowSoftInputOnFocus(false);
    

    Credits to Chen Su article.

    0 讨论(0)
  • 2020-12-03 05:35

    The exact functionality that you require is provided by setting the flag textIsSelectable in EditText to true. With this, the cursor will still be present, and you'll be able to select/copy/cut/paste, but SoftKeyboard will never show. Requires API 11 and above.

    You can set it in your xml layout like this:

    <EditText
        android:textIsSelectable="true"
        ...
    />
    

    Or programmatically, like this:

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

    For anyone using API 10 and below, hack is provided here : https://stackoverflow.com/a/20173020/7550472

    0 讨论(0)
  • 2020-12-03 05:38

    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.

    0 讨论(0)
  • 2020-12-03 05:39

    This works perfectly (for me) in 2 steps:

    1. <activity... android:windowSoftInputMode="stateHidden"> in manifest file

    2. Add these properties in your editText XML code

      android:focusable="true"
      android:focusableInTouchMode="true
      

    You have to put both 1 and 2, only then it will work.

    Cheers

    0 讨论(0)
  • 2020-12-03 05:41

    First add android:windowSoftInputMode="stateHidden" in your manifest file, under the activity. like this

    <activity... android:windowSoftInputMode="stateHidden">
    

    The on your xml add this android:textIsSelectable="true" . This will make the pointer visible.

    Then on onCreate method of the activity, add this:

    EditText editText = (EditText)findViewById(R.id.edit_text);
    edit_text.setOnTouchListener(new OnTouchListener() {
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.onTouchEvent(event);
            InputMethodManager inputMethod = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (inputMethod!= null) {
                inputMethod.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }                
            return true;
        }
    });
    
    0 讨论(0)
  • 2020-12-03 05:44

    Best solution from @Lupsaa here:

    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.

    0 讨论(0)
提交回复
热议问题