简体   繁体   中英

EditText: suppress soft keyboard but allow selection and cursor?

I want to display an EditText which will be edited mostly by selecting parts of it and pressing buttons to insert new values. (The values are long hex strings which I don't want the user to have to type.) Therefore, I want to suppress the display of the soft keyboard unless the user deliberately calls it up.

This question describes almost exactly the same requirement, but the accepted answer does not work on Android 5.1. When the input mode is changed to InputType.TYPE_NULL , the EditText disables all editing functionality, not just the soft keyboard. There's no cursor, no click-drag selection, etc.

Overriding onCheckIsTextEditor() as suggested in this answer behaves the same way.

Is there any way to accomplish this on Android 5?

This was added in API 21:

EditText editText = (EditText) findViewById(R.id.edittext);
editText.setShowSoftInputOnFocus(false);

According to grepcode , this method has been present but hidden since version 4.1.1. So you can annotate the method that calls it as @TargetApi(Build.VERSION_CODES.LOLLIPOP) , and protect the call itself with if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM