1

当我的应用程序启动时,我在 EditText 中加载了一些用户无法编辑的文本,直到单击“菜单”并选择“编辑文本”。初始 xml 如下所示:

<EditText
    android:id="@+id/myTextBox"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:cursorVisible="true"
    android:gravity="top"
    android:inputType="none"
    android:textIsSelectable="true"
    android:scrollbars="vertical"
    android:maxLines="1"
     />

用户选择“编辑文本”后,如何使其再次可编辑?我尝试在代码中执行以下操作:

_mytextBox.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_MULTI_LINE);
_mytextBox.setTextIsSelectable(false);

但是键盘再也没有出现过。如果我从 xml 中删除 textIsSelectable 并使用 android:inputType="textMultiLine",它就像我想要的那样工作。

附加信息:抱歉忘了提及,我不能使用禁用/启用方式,因为我希望用户能够选择其中的任何文本并进行“复制”。此外,禁用它会使文本变暗..这不是我想要的。谢谢!

我本来希望调用 _mytextBox.setTextIsSelectable(false); 会将一切重置为正常,但它不会.. 似乎是一个错误?可能与此问题有关:https ://code.google.com/p/android/issues/detail?id=27609

4

4 回答 4

2

要重置 setTextIsSelectable,您只需将一组 EditText 属性恢复为原始状态...

这是我开发用于我的应用程序的 Utils 方法..

public static void toggleSelectableState(EditText editText){
    if(editText.isTextSelectable()){
        editText.setTextIsSelectable(false);
        editText.setMovementMethod(ArrowKeyMovementMethod.getInstance());
        editText.setCursorVisible(true);
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.setClickable(true);
        editText.setLongClickable(true);
    }else{
        editText.setTextIsSelectable(true);
        editText.setCursorVisible(false);
    }
}

不客气...

于 2015-10-14T19:44:34.710 回答
1

用于启用EditText使用

_mytextBox.setEnabled(true);

和禁用EditText使用

_mytextBox.setEnabled(false);

如果您不想在上面使用,请尝试在 edittext 上隐藏/显示键盘

       // Show soft keyboard for the user to enter the value.
       InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
       imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);




        // Hide soft keyboard.
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
于 2013-10-20T14:44:25.383 回答
0

在你的xml中制作你的EditText最初 android:enabled="false"

并在项目上onOptionsItemSelected编辑文本选项使其setEnabled(true)

@Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case R.id.edit_text_option_id:
            editText.setEnabled(true);
                return true;
        default:
            return super.onOptionsItemSelected(item);

        }

    }
于 2013-10-20T14:43:38.407 回答
0

您是否尝试过 requestFocus 来编辑文本?

于 2013-10-20T14:43:48.707 回答