Make EditText ReadOnly

前端 未结 22 1245

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:15

    This worked for me, taking several of the suggestions above into account. Makes the TextEdit focusable, but if user clicks or focuses, we show a list of selections in a PopupWindow. (We are replacing the wacky Spinner widget). TextEdit xml is very generic...

    public void onCreate(Bundle savedInstanceState) {
        ....  
        fEditState = (EditText) findViewById(R.id.state_edit);
    
        fEditState.setLongClickable(false);
        fEditState.setKeyListener(null);
        fEditState.setFocusable(true);
    
        fEditState.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus)
            {
                if (hasFocus)
                {
                   showStatesPopup();
    
                }
            }
        });
    
        fEditState.setOnClickListener(new Button.OnClickListener(){
    
            public void onClick(View arg0)
            {
               showStatesPopup();
            }
        });
        ....
    }
    
    private void showStatesPopup()
    {
        // fPopupWindowStates instantiated in OnCreate()
    
        if (!fPopupWindowStates.isShowing()) {
            // show the list view as dropdown
            fPopupWindowStates.showAsDropDown(fEditState, -5, 0);
        }
    }  
    
    0 讨论(0)
  • 2020-12-04 19:16

    Try overriding the onLongClick listener of the edit text to remove context menu:

    EditText myTextField = (EditText)findViewById(R.id.my_edit_text_id);
    myTextField.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            return true;
        }
    });
    
    0 讨论(0)
  • 2020-12-04 19:16

    Use this code:

    editText.setEnabled(false);
    editText.setTextColor(ContextCompat.getColor(context, R.color.black);
    

    Disabling editText gives a read-only look and behavior but also changes the text-color to gray so setting its text color is needed.

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

    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);
    
    0 讨论(0)
  • 2020-12-04 19:17

    set in XML

    android:inputType="none" 
    
    0 讨论(0)
  • 2020-12-04 19:20

    This was the only full simple solution for me.

    editText.setEnabled(false);   // Prevents data entry
    editText.setFocusable(false); // Prevents being able to tab to it from keyboard
    
    0 讨论(0)
提交回复
热议问题