Make EditText ReadOnly

前端 未结 22 1243

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.

H

相关标签:
22条回答
  • 2020-12-04 19:10

    If you just want to be able to copy text from the control but not be able to edit it you might want to use a TextView instead and set text is selectable.

    code:

    myTextView.setTextIsSelectable(true);
    myTextView.setFocusable(true);
    myTextView.setFocusableInTouchMode(true);
    // myTextView.setSelectAllOnFocus(true);
    

    xml:

    <TextView
        android:textIsSelectable="true"
        android:focusable="true"
        android:focusableInTouchMode="true"     
        ...
    />
    <!--android:selectAllOnFocus="true"-->
    


    The documentation of setTextIsSelectable says:

    When you call this method to set the value of textIsSelectable, it sets the flags focusable, focusableInTouchMode, clickable, and longClickable to the same value...

    However I had to explicitly set focusable and focusableInTouchMode to true to make it work with touch input.

    0 讨论(0)
  • 2020-12-04 19:11

    Set this in EdiTextView xml file

    android:focusable="false"
    
    0 讨论(0)
  • 2020-12-04 19:12

    In XML use:

    android:editable="false"
    

    As an example:

    <EditText
        android:id="@+id/EditText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:editable="false" />
    
    0 讨论(0)
  • 2020-12-04 19:12
    editText.setInputType(InputType.TYPE_NULL);
    

    As per the docs this prevents the soft keyboard from being displayed. It also prevents pasting, allows scrolling and doesn't alter the visual aspect of the view. However, this also prevents selecting and copying of the text within the view.

    From my tests setting setInputType to TYPE_NULL seems to be functionally equivalent to the depreciated android:editable="false". Additionally, android:inputType="none" seems to have no noticeable effect.

    0 讨论(0)
  • 2020-12-04 19:14

    My approach to this has been creating a custom TextWatcher class as follows:

    class ReadOnlyTextWatcher implements TextWatcher {
        private final EditText textEdit;
        private String originalText;
        private boolean mustUndo = true;
    
        public ReadOnlyTextWatcher(EditText textEdit) {
            this.textEdit = textEdit;
        }
    
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (mustUndo) {
                originalText = charSequence.toString();
            }
        }
    
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
        }
    
        @Override
        public void afterTextChanged(Editable editable) {
            if (mustUndo) {
                mustUndo = false;
                textEdit.setText(originalText);
            } else {
                mustUndo = true;
            }
        }
    }
    

    Then you just add that watcher to any field you want to be read only despite being enabled:

    editText.addTextChangedListener(new ReadOnlyTextWatcher(editText));
    
    0 讨论(0)
  • 2020-12-04 19:15
    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.

    0 讨论(0)
提交回复
热议问题