EditText not editable

2019-01-22 05:34发布

I'm trying to put a EditText as not editable with this code:

<EditText android:id="@+id/outputResult"
          android:inputType="text"
          android:editable="false"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="@string/result" />

I had to add this for not editable

android:focusable="false" 

Am I doing something wrong?

Thanks you verry much

8条回答
\"骚年 ilove
2楼-- · 2019-01-22 06:33

Use This:

EditText edtText = (EditText) findViewById(R.id.yourEditTextId);

edtText.setEnabled(false);

also you can use this in your xml file.

查看更多
Viruses.
3楼-- · 2019-01-22 06:36

android:editable="false" should work, but it is deprecated, you should be using android:inputType="none" instead.

Alternatively, if you want to do it in the code you could do this :

EditText mEdit = (EditText) findViewById(R.id.yourid);
mEdit.setEnabled(false);

This is also a viable alternative :

EditText mEdit = (EditText) findViewById(R.id.yourid);
mEdit.setKeyListener(null);

If you're going to make your EditText non-editable, may I suggest using the TextView widget instead of the EditText, since using a EditText seems kind of pointless in that case.

EDIT: Altered some information since I've found that android:editable is deprecated, and you should use android:inputType="none", but there is a bug about it on android code; So please check this.

查看更多
登录 后发表回答