How to set editable true/false EditText in Android programmatically?

前端 未结 12 1184
予麋鹿
予麋鹿 2020-12-09 02:04

We can set editable property of EditText in XML layout but not programatically, but there is no setEditable() method!

If EditText

相关标签:
12条回答
  • 2020-12-09 02:26

    I did it in a easier way , setEditable and setFocusable false. but you should check this.

    How to replicate android:editable="false" in code?

    0 讨论(0)
  • 2020-12-09 02:30

    Since setEditable(false) is deprecated, use textView.setKeyListener(null); to make editText non-clickable.

    0 讨论(0)
  • 2020-12-09 02:32

    How to do it programatically :

    To enable EditText use:

    et.setEnabled(true);
    

    To disable EditText use:

    et.setEnabled(false);
    
    0 讨论(0)
  • 2020-12-09 02:35

    This may help:

    if (cbProhibitEditPW.isChecked()) { // disable editing password
           editTextPassword.setFocusable(false);
           editTextPassword.setFocusableInTouchMode(false); // user touches widget on phone with touch screen
           editTextPassword.setClickable(false); // user navigates with wheel and selects widget
           isProhibitEditPassword= true;
    } else { // enable editing of password
           editTextPassword.setFocusable(true);
           editTextPassword.setFocusableInTouchMode(true);
           editTextPassword.setClickable(true);
           isProhibitEditPassword= false;
    }
    
    0 讨论(0)
  • 2020-12-09 02:41
    editText.setInputType(InputType.TYPE_NULL);
    
    0 讨论(0)
  • 2020-12-09 02:41

    Once focus of edit text is removed, it would not allow you to type even if you set it to focusable again.

    Here is a way around it

    if (someCondition)
       editTextField.setFocusable(false);
    else
       editTextField.setFocusableInTouchMode(true);
    

    Setting it true in setFocusableInTouchMode() seems to do the trick.

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