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
.
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
.
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" .
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"
/>
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);
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"
.
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);
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"
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.
Use
android:inputType="none"
instead of
android:editable="false"
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
Can be set it in code like this:
myEditText.inputType = InputType.TYPE_NULL
© 2022 - 2024 — McMap. All rights reserved.
inputType="none"
, it still showed a cursor on the field. I had to seteditable="false"
to make it go away, anyway. – Charmer