EditText不可编辑

Jjr*_*ina 49 android android-edittext

我试图使用此代码将EditText设置为不可编辑:

<EditText android:id="@+id/outputResult"
          android:inputType="text"
          android:editable="false"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="@string/result" />
Run Code Online (Sandbox Code Playgroud)

我不得不添加这个不可编辑

android:focusable="false" 
Run Code Online (Sandbox Code Playgroud)

难道我做错了什么?

非常感谢你

Jea*_*Roy 130

android:editable="false"应该工作,但它已被弃用,你应该使用android:inputType="none".

或者,如果您想在代码中执行此操作,则可以执行以下操作:

EditText mEdit = (EditText) findViewById(R.id.yourid);
mEdit.setEnabled(false);
Run Code Online (Sandbox Code Playgroud)

这也是一个可行的选择:

EditText mEdit = (EditText) findViewById(R.id.yourid);
mEdit.setKeyListener(null);
Run Code Online (Sandbox Code Playgroud)

如果您要进行EditText不可编辑的操作,我可以建议使用TextView小部件而不是小部件EditText,因为在这种情况下使用EditText似乎毫无意义.

编辑:改变了一些信息,因为我发现它android:editable已被弃用,你应该使用android:inputType="none",但在android代码上有一个关于它的错误; 所以请检查一下.

  • `android:inputType="none"` 仍然允许您输入文本 (8认同)
  • 在这种情况下,使用textview会更好 (6认同)

Dha*_*hra 10

 <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editTexteventdate"
            android:background="@null"
            android:hint="Date of Event"
            android:textSize="18dp"
            android:textColor="#fff"
            android:editable="false"
            android:focusable="false"
            android:clickable="false"

            />
Run Code Online (Sandbox Code Playgroud)

加上这个

机器人:编辑="假"

机器人:可聚焦="假"

机器人:可点击="假"

它为我工作


vov*_*ost 9

如果您想使EditText不能从代码中编辑:

void disableInput(EditText editText){
    editText.setInputType(InputType.TYPE_NULL);
    editText.setTextIsSelectable(false);
    editText.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v,int keyCode,KeyEvent event) {
                return true;  // Blocks input from hardware keyboards.
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

如果你想删除EditText的水平线,只需添加:

editText.setBackground(null);
Run Code Online (Sandbox Code Playgroud)

如果您想让用户复制EditText的内容,请使用:

editText.setTextIsSelectable(true);
Run Code Online (Sandbox Code Playgroud)


Aks*_*aru 8

我想我来晚了,但是请结合使用以下内容使其有效:

 android:inputType="none"
 android:enabled="false"
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案。使用 `android:inputType="none"` 不会是您所期望的,除非您添加 `android:enabled="false"` (3认同)

小智 8

wellandroid:editable="false"已被弃用,不再工作,并且android:inputType="none"不会给您想要的结果

你可以用

<EditText
            android:cursorVisible="false"
            android:focusableInTouchMode="false"
            android:maxLength="10"/>
Run Code Online (Sandbox Code Playgroud)

如果您想让 EditText 再次可编辑,您可以使用代码来做到这一点

//enable view's focus event on touch mode
edittext.setFocusableInTouchMode(true);

//enable cursor is visible
edittext.setCursorVisible(true);

// You have to focus on the edit text to avoid having to click it twice
//request focus 
edittext.requestFocus();

//show keypad is used to allow the user input text on the first click
InputMethodManager openKeyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 

openKeyboard.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT);
Run Code Online (Sandbox Code Playgroud)

注意我正在使用android:focusableInTouchMode="false"而不是

android:focusable="false" 
Run Code Online (Sandbox Code Playgroud)

因为当我使用

edittext.setFocusable(true); 
Run Code Online (Sandbox Code Playgroud)

我还得补充一下

edittext.setFocusableInTouchMode(true); 
Run Code Online (Sandbox Code Playgroud)

像这样

edittext.setFocusableInTouchMode(true);
edittext.setFocusable(true);
Run Code Online (Sandbox Code Playgroud)

再次启用编辑文本焦点


Mof*_*egi 7

如果您确实要使用editText而不是textView,请考虑使用以下三种方法:

android:focusable="false"

android:clickable="false"

android:longClickable="false"
Run Code Online (Sandbox Code Playgroud)

编辑:“ longClickable”属性禁用导致“粘贴”和/或“全选”选项显示为工具提示的长按操作。