setTextIsSelectable如何防止键盘出现? [英] How does setTextIsSelectable prevent the keyboard from appearing?

查看:243
本文介绍了setTextIsSelectable如何防止键盘出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用包含一个EditText的单个Activity创建一个简单的应用程序,那么我会这样做

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

然后防止键盘出现(在我对Android 5.0和7.1的测试中).这些问题是我想要的:

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

源代码

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.

无论是否设置setTextIsSelectable,调用以下方法都将返回true.

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.

更新

后续问答:

推荐答案

当您调用TextView.setTextIsSelectable(true)然后由于

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

我猜想为什么要以这种方式实现它,可能是因为框架团队决定他们希望文本Editor可以预测为可选择的,以免键盘在屏幕上移动而在屏幕上跳动.这可能只是一个UI决定.根据git的说法,自2012年以来就是这种方式,但是该提交未提及有关该实现的任何具体内容.

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.

这篇关于setTextIsSelectable如何防止键盘出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆