How to enable keyboard on touch after disabling it with setTextIsSelectable

后端 未结 1 1395
失恋的感觉
失恋的感觉 2021-01-14 23:08

I am using a custom in-app keyboard, so I need to disable the system keyboard. I can do that with

editText.setShowSoftInputOnFocus(false);

相关标签:
1条回答
  • 2021-01-14 23:37

    Short answer

    Doing the following will reverse the effects of setTextIsSelectable(true) and allow the keyboard to show again when the EditText receives focus.

    editText.setTextIsSelectable(false);
    editText.setFocusable(true);
    editText.setFocusableInTouchMode(true);
    editText.setClickable(true);
    editText.setLongClickable(true);
    editText.setMovementMethod(ArrowKeyMovementMethod.getInstance());
    editText.setText(editText.getText(), TextView.BufferType.SPANNABLE);
    

    Explanation

    The thing that prevents the keyboard from showing is isTextSelectable() being true. You can see that here (thanks to @adneal).

    The source code for setTextIsSelectable is

    public void setTextIsSelectable(boolean selectable) {
        if (!selectable && mEditor == null) return; // false is default value with no edit data
    
        createEditorIfNeeded();
        if (mEditor.mTextIsSelectable == selectable) return;
    
        mEditor.mTextIsSelectable = selectable;
        setFocusableInTouchMode(selectable);
        setFocusable(selectable);
        setClickable(selectable);
        setLongClickable(selectable);
    
        // mInputType should already be EditorInfo.TYPE_NULL and mInput should be null
    
        setMovementMethod(selectable ? ArrowKeyMovementMethod.getInstance() : null);
        setText(mText, selectable ? BufferType.SPANNABLE : BufferType.NORMAL);
    
        // Called by setText above, but safer in case of future code changes
        mEditor.prepareCursorControllers();
    }
    

    Thus, the code in the short answer section above first sets mTextIsSelectable to false with setTextIsSelectable(false) and then undoes all of the other side effects one-by-one.

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