How to set editable true/false EditText in Android

2019-01-17 14:33发布

问题:

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

If EditText is not Enabled [ by setEnabled(false)] it still Editable!

回答1:

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

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



回答2:

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;
}


回答3:

Fetch the KeyListener value of EditText by editText.getKeyListener() and store in the KeyListener type variable, which will contain the Editable property value:

KeyListener variable;
variable = editText.getKeyListener(); 

Set the Editable property of EditText to false as:

 edittext.setKeyListener(null);

Now set Editable property of EditText to true as:

editText.setKeyListener(variable);  

Note: In XML the default Editable property of EditText should be true.



回答4:

editText.setInputType(InputType.TYPE_NULL);


回答5:

How to do it programatically :

To enable EditText use:

et.setEnabled(true);

To disable EditText use:

et.setEnabled(false);


回答6:

hope this one helps you out:

edittext1.setKeyListener(null);
edittext1.setCursorVisible(false);
edittext1.setPressed(false);
edittext1.setFocusable(false);


回答7:

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.



回答8:

try this,

EditText editText=(EditText)findViewById(R.id.editText1);

editText.setKeyListener(null);

It works fine...



回答9:

editText.setFocusable(false);
editText.setClickable(false);


回答10:

Try this it is working fine for me..

EditText.setInputType(0);
EditText.setFilters(new InputFilter[] {new InputFilter()
{
@Override
public CharSequence filter(CharSequence source, int start,
                            int end, Spanned dest, int dstart, int dend) 
{
return source.length() < 1 ? dest.subSequence(dstart, dend) : "";

}
}
});


回答11:

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



回答12:

An easy and safe method:

editText.clearFocus();
editText.setFocusable(false);