Make EditText ReadOnly

2019-01-21 18:00发布

I want to make a read-only EditText view. The XML to do this code seems to be android:editable="false", but I want to do this in code.

How can I do this?

19条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-21 18:34
android:editable

If set, specifies that this TextView has an input method. It will be a textual one unless it has otherwise been specified. For TextView, this is false by default. For EditText, it is true by default.

Must be a boolean value, either true or false.

This may also be a reference to a resource (in the form @[package:]type:name) or theme attribute (in the form ?[package:][type:]name) containing a value of this type.

This corresponds to the global attribute resource symbol editable.

Related Methods

查看更多
Lonely孤独者°
3楼-- · 2019-01-21 18:38

this is my implementation (a little long, but useful to me!): With this code you can make EditView Read-only or Normal. even in read-only state, the text can be copied by user. you can change the backgroud to make it look different from a normal EditText.

public static TextWatcher setReadOnly(final EditText edt, final boolean readOnlyState, TextWatcher remove) {
    edt.setCursorVisible(!readOnlyState);
    TextWatcher tw = null;
    final String text = edt.getText().toString();
    if (readOnlyState) {
            tw = new TextWatcher();

            @Override
            public void afterTextChanged(Editable s) {

            }
            @Override
            //saving the text before change
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            // and replace it with content if it is about to change
            public void onTextChanged(CharSequence s, int start,int before, int count) {
                edt.removeTextChangedListener(this);
                edt.setText(text);
                edt.addTextChangedListener(this);
            }
        };
        edt.addTextChangedListener(tw);
        return tw;
    } else {
        edt.removeTextChangedListener(remove);
        return remove;
    }
}

the benefit of this code is that, the EditText is displayed as normal EditText but the content is not changeable. The return value should be kept as a variable to one be able revert back from read-only state to normal.

to make an EditText read-only, just put it as:

TextWatcher tw = setReadOnly(editText, true, null);

and to make it normal use tw from previous statement:

setReadOnly(editText, false, tw);
查看更多
太酷不给撩
4楼-- · 2019-01-21 18:40
editText.setEnabled(false);
editText.setFilters(new InputFilter[] { new InputFilter() {
    public CharSequence filter(CharSequence src, int start, int end,
            Spanned dst, int dstart, int dend) {
        return src.length() < 1 ? dst.subSequence(dstart, dend) : "";
    }
} });

This will give you uneditable EditText filter. you first need to put the text you want on the editText field and then apply this filter.

查看更多
神经病院院长
5楼-- · 2019-01-21 18:43

If you setEnabled(false) then your editText would look disabled (gray, etc). You may not want to change the visual aspect of your editor.

A less intrusive way would be to use setFocusable(false).

I believe that this answers your question closer to your initial intent.

查看更多
太酷不给撩
6楼-- · 2019-01-21 18:43

android:editable="false" has been depreciated. Therefore you cant use it to make the edit text readonly.

I have done this using the bellow solution. Here I have used

android:inputType="none"

android:textIsSelectable="true"

android:focusable="false"

Give it try :)

<EditText
         android:id="@+id/et_newsgpa_university"
         android:layout_height="wrap_content"
         android:layout_width="match_parent"
         android:hint="@string/hint_educational_institute"
         android:textSize="@dimen/regular_text"
         android:inputType="none"
         android:textIsSelectable="true"
         android:focusable="false"
         android:maxLines="1"
         android:imeOptions="actionNext"/>
查看更多
贪生不怕死
7楼-- · 2019-01-21 18:46

set in XML

android:inputType="none" 
查看更多
登录 后发表回答