How to set editable true/false EditText in Android

2019-01-17 14:20发布

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!

12条回答
放我归山
2楼-- · 2019-01-17 14:58

try this,

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

editText.setKeyListener(null);

It works fine...

查看更多
走好不送
3楼-- · 2019-01-17 14:58

An easy and safe method:

editText.clearFocus();
editText.setFocusable(false);
查看更多
Emotional °昔
4楼-- · 2019-01-17 15:00

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;
}
查看更多
淡お忘
5楼-- · 2019-01-17 15:03
editText.setInputType(InputType.TYPE_NULL);
查看更多
可以哭但决不认输i
6楼-- · 2019-01-17 15:05

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) : "";

}
}
});
查看更多
仙女界的扛把子
7楼-- · 2019-01-17 15:10

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

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

查看更多
登录 后发表回答