使用setTextIsSelectable禁用键盘后如何启用触摸键盘 [英] How to enable keyboard on touch after disabling it with setTextIsSelectable

查看:110
本文介绍了使用setTextIsSelectable禁用键盘后如何启用触摸键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义应用内键盘,因此我需要禁用系统键盘.我可以使用

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

editText.setShowSoftInputOnFocus(false);

对于Android API 21+.但是要做到API 11一样的事情,我正在做

for Android API 21+. But to do the same thing down to API 11, I am doing

editText.setTextIsSelectable(true);

有时我想用setTextIsSelectable禁用它后再次显示系统键盘.但是我不知道怎么做.进行以下操作确实会显示系统键盘,但是如果用户隐藏了键盘,然后再次单击EditText,则仍然不会显示键盘.

Sometimes I want to show the system keyboard again after disabling it with setTextIsSelectable. But I can't figure out how. Doing the following does show the system keyboard, but if the user hides the keyboard and then clicks the EditText again, the keyboard still won't show.

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, 0);

我想我可以做editText.setOnFocusChangeListener,然后手动显示或隐藏系统键盘,但是我宁愿撤消setTextIsSelectable所做的任何事情.以下内容也不起作用:

I guess I could do editText.setOnFocusChangeListener and then manually show or hide the system keyboard, but I would prefer to undo whatever setTextIsSelectable did. The following also does not work:

editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.setClickable(true);
editText.setLongClickable(true);

我该怎么做?

相关问题

推荐答案

简短答案

执行以下操作将反转setTextIsSelectable(true)的效果,并在EditText获得焦点时再次显示键盘.

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

说明

阻止键盘显示的是isTextSelectable()true.您可以看到此处(感谢 @adneal ).

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

setTextIsSelectable的源代码为

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

因此,上面简短回答部分中的代码首先将mTextIsSelectable设置为falsesetTextIsSelectable(false),然后逐个消除所有其他副作用.

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.

这篇关于使用setTextIsSelectable禁用键盘后如何启用触摸键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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