How does setTextIsSelectable prevent the keyboard from appearing?

做~自己de王妃 提交于 2019-12-11 14:59:01

问题


If I create a simple app with single Activity that contains a single EditText, and I do

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

then this prevents the keyboard from appearing (in my tests on Android 5.0 and 7.1). This is what I want, as requested in these questions:

  • Disable soft-keyboard from EditText but still allow copy/paste?
  • How to disable Android Soft Keyboard for a particular activity?
  • Android: Disable soft keyboard at all EditTexts
  • How to disable keypad popup when on edittext?
  • Disable keyboard on EditText

The source code 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();
}

But I don't see what about this disables the Input Method from appearing.

Calling the following methods all return true whether I set setTextIsSelectable or not.

editText.isFocusable();             // true
editText.isFocusableInTouchMode();  // true
editText.isClickable();             // true
editText.isLongClickable();         // true

I'm partly asking because I'm curious, but also because I need to disable the system keyboard in my app. I want to understand what is happening so that I can be sure that it is really doing what I think it is doing.

Update

Follow-up question and answer:

  • How to enable keyboard on touch after disabling it with setTextIsSelectable

回答1:


The keyboard isn't shown when you call TextView.setTextIsSelectable(true) and then select text as a result of Editor.startSelectionActionModeInternal, which checks if TextView.isTextSelectable is false before showing the text selection ActionMode.

    final boolean selectionStarted = mTextActionMode != null;
    if (selectionStarted && !mTextView.isTextSelectable() && mShowSoftInputOnFocus) {
        // Show the IME to be able to replace text, except when selecting non editable text.
        final InputMethodManager imm = InputMethodManager.peekInstance();
        if (imm != null) {
            imm.showSoftInput(mTextView, 0, null);
        }
    }

My guess to why it's implemented this way is probably because the framework team decided they wanted text Editor could predict to be selectable to not jump around the screen as a result of the keyboard moving it around. It was probably just a UI decision. According to git it's been that way since 2012, but the commit doesn't mention anything specifically regarding that implementation.



来源:https://stackoverflow.com/questions/45227805/how-does-settextisselectable-prevent-the-keyboard-from-appearing

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