2017-07-21 172 views
1

我使用的是custom in-app keyboard,所以我需要禁用系统键盘。我可以用如何使用setTextIsSelectable禁用键盘后启用键盘

editText.setShowSoftInputOnFocus(false); 

对于Android API 21+。但要做到同样的事情到API 11,我做

editText.setTextIsSelectable(true); 

有时候我想与setTextIsSelectable禁用后再次显示系统键盘。但我无法弄清楚如何。执行以下操作将显示系统键盘,但如果用户隐藏键盘,然后再次单击EditText,则键盘仍不会显示。

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

我想我能做到editText.setOnFocusChangeListener,然后手动显示或隐藏系统键盘,但我宁愿取消任何setTextIsSelectable一样。以下也不起作用:

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

我该怎么办?

Related question

回答

1

简短的回答

执行以下将扭转setTextIsSelectable(true)的影响,并允许在EditText接收焦点键盘再次显示。

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的事情。您可以看到here(感谢@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(); 
} 

因此,在短答案部上面的代码首先设置mTextIsSelectablefalsesetTextIsSelectable(false),然后撤消所有的其他副作用一个接一个。