Android Hide Soft Keyboard from EditText while not losing cursor

瘦欲@ 提交于 2019-11-27 13:34:09

This worked for me:

        // Update the EditText so it won't popup Android's own keyboard, since I have my own.
    EditText editText = (EditText)findViewById(R.id.edit_mine);
    editText.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.onTouchEvent(event);
            InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }                
            return true;
        }
    });

I have finally found a (for me) working solution to this.

First part (in onCreate):

// Set to TYPE_NULL on all Android API versions
mText.setInputType(InputType.TYPE_NULL);
// for later than GB only
if (android.os.Build.VERSION.SDK_INT >= 11) {
    // this fakes the TextView (which actually handles cursor drawing)
    // into drawing the cursor even though you've disabled soft input
    // with TYPE_NULL
    mText.setRawInputType(InputType.TYPE_CLASS_TEXT);
}

In addition, android:textIsSelectable needs to be set to true (or set in onCreate) and the EditText must not be focused on initialization. If your EditText is the first focusable View (which it was in my case), you can work around this by putting this just above it:

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

You can see the results of this in the Grapher application, free and available in Google Play.

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.

Droid Diva

use

android:windowSoftInputMode="stateHidden" 

in your manifest file instead of android:windowSoftInputMode="stateAlwaysHidden"

This is what I did. First, in manifest inside activity

android:windowSoftInputMode="stateAlwaysHidden|adjustNothing"

Second, in onCreate if inside activity or onActivityCreated if inside fragment

editText.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            hideSoftKeyboard(v);
        }
    });

Do not forget to request focus to the editText

editText.requestFocus();

Then add the hideSoftKeyboard(v) method same as the other answer.

private void hideSoftKeyboard(View v){
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}

The key here is to requestFocus before clicking the EditText. If without focus, first click will make the keyboard show up(my experience). However, this is applied if you have a single EditText in an activity. With this, you still can type with custom keyboard(if any), can copy and paste, and cursor is still visible.

Kaushik NP

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

Santacrab

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.

    EditText text = (EditText) findViewById(R.id.text);
    if (Build.VERSION.SDK_INT >= 11) {
        text.setRawInputType(InputType.TYPE_CLASS_TEXT);
        text.setTextIsSelectable(true);
    } else {
        text.setRawInputType(InputType.TYPE_NULL);
        text.setFocusable(true);
    }
user2144695

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

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;
    }
});

EditText editText = (EditText)findViewById(R.id.edit_mine); editText.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.onTouchEvent(event);
        InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }                
        return true;
    }
});

ha... this is the correct way of doing...this job done... this gonna work !

You can use the following line of code in the activity's onCreate method to make sure the keyboard only pops up when a user clicks or touch into an EditText Field. I tried lots of methods and codes from stackoverflow but didnt work any but this Works Perfectly for me!! Try this.. :)`

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

You can use the following line of code in the activity's onCreate method to make sure the keyboard only pops up when a user clicks or touch into an EditText Field. I tried lots of methods and codes from stackoverflow but didnt work any but this Works Perfectly for me!! Try this.. :)`

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