27

I want to realize

android:editable="false"

But it told me editable is deprecated ,you can use inputType instead.

So I don't know how to realize it by using inputType.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
Wu Zijan
  • 383
  • 1
  • 3
  • 8

9 Answers9

20

Use

android:clickable="false"

. but In case you want to use onclick listener for it. don't use

android:clickable="false"

.. Use

android:cursorVisible="false" android:focusable="false" .

SilenceCodder
  • 2,874
  • 24
  • 28
10

The Code

In XML

<EditText
    android:id="@+id/myEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Hint"
    android:focusable="false"
    android:clickable="false"
    android:cursorVisible="false"
    />

The Result

enter image description here

Java Code

You can achieve the same result at runtime with this code

EditText et = findViewById(R.id.myEditText);
et.setFocusable(false);
et.setClickable(false);
et.setCursorVisible(false);
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
3

If you are using any input type that is not kind of text or number like date or date and time you can use android:inputType="date" or android:inputType="datetime" or android:inputType="time". If you are not using any such kind of input type you can use android:inputType="none".

Saurabh
  • 41
  • 3
3

All the answers above suggesting XML markup changes doesnt seem to work. However I managed to achieve a similar result through code.

Simply use:

editText.setKeyListener(null);

Saminda Peramuna
  • 735
  • 9
  • 22
1

Use this if you wanted to use click of edittext and disable input

android:clickable="false" 
android:cursorVisible="false" 
android:focusable="false" 
android:focusableInTouchMode="false"
Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82
0

You can control behavior of EditText from TextWatcher

@Override
public void onTextChanged(String text) {
     setEditTextCurrentValue((isEditTextEditable())?text:getEditTextCurrentValue());
}

So user can enter to the control, can copy its content. But he can't change it if you forbid.

0

Use

android:inputType="none"

instead of

android:editable="false"
Divy Soni
  • 824
  • 9
  • 22
0

well input type doesn't work the way you expect it to work when you use inputType=none

but you could use this to make your editText not editable which is what I think you want to achieve

<EditText
            android:cursorVisible="false"
            android:focusableInTouchMode="false"
            android:maxLength="10"/>

and if you want to make the EditText editable again you could do that with code

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

Note I'm using android:focusableInTouchMode="false" and not

android:focusable="false" 

because when I use

edittext.setFocusable(true); 

I still have to add

edittext.setFocusableInTouchMode(true); 

Like this

edittext.setFocusableInTouchMode(true);
edittext.setFocusable(true);

for the edittext focus to be enabled again

Olayiwola Osho
  • 121
  • 2
  • 4
0

Can be set it in code like this:

myEditText.inputType = InputType.TYPE_NULL
nGL
  • 333
  • 3
  • 12