Disable soft keyboard for Edittext

I have an EditText where I want to process the input myself, so I don’t want the soft keyboard to appear when I click on it (or when the selection changes, focus changes, long press, etc.). However, I still want to be able to select the text, reposition the cursor, copy / past, etc.

I tried putting android: windowSoftInputMode = "stateAlwaysHidden" in the manifest, but that does not seem to do much. I also tried adding the following

edittext.setOnTouchListener(new OnTouchListener() {
    @Override public boolean onTouch(View v, MotionEvent event) {
        EditText edittext = (EditText) v;
        int inType = edittext.getInputType();       // Backup the input type
        edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard
        edittext.onTouchEvent(event);               // Call native handler
        edittext.setInputType(inType);              // Restore input type
        return true; // Consume touch event
    }
});

which disables the keyboard and also obstructs the cursor.

Currently, I'm basically trying to add listeners for all situations where a keyboard may appear to switch it, but it is very awkward, and I cannot catch all the cases. Is there a better way to disable the soft keyboard for a specific EditText or fragment?

+4
source share
1 answer

Try this code.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Disable IME for this application
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

        setContentView(R.layout.activity_layout);
0
source

All Articles