Disabling of EditText in Android

2020-01-25 03:43发布

In my application, I have an EditText that the user only has Read access not Write access.

In code I set android:enabled="false".

Although the background of EditText changed to dark, when I click on it the keyboard pops up and I can change the text.

What should I set to disable EditText?

23条回答
我命由我不由天
2楼-- · 2020-01-25 04:06

This works for me:

android:focusable="false"

查看更多
We Are One
3楼-- · 2020-01-25 04:07

Simply:

editText.setEnabled(false);
查看更多
Root(大扎)
4楼-- · 2020-01-25 04:07

From @Asymptote's comment on the accepted answer, use:

myEditText.setEnabled(false);
myEditText.setInputType(InputType.TYPE_NULL);

...and Bob's your uncle.

查看更多
Rolldiameter
5楼-- · 2020-01-25 04:08

Use this to disable user input

android:focusable="false"

android:editable="false" This method is deprecated one.

查看更多
混吃等死
6楼-- · 2020-01-25 04:10

Set below properties in class:

editText.setFocusable(false);
editText.setEnabled(false);

It will work smoothly as you required.

查看更多
Viruses.
7楼-- · 2020-01-25 04:11

Try this one, works fine for me:

public class CustomEdittext extends EditText {

Boolean mIsTextEditor=true;
public CustomEdittext(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
public boolean onCheckIsTextEditor() {
    // TODO Auto-generated method stub
    return mIsTextEditor;
}


@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    mIsTextEditor=false;
    Boolean mOnTouchEvent=super.onTouchEvent(event);
    mIsTextEditor=true;     
    return mOnTouchEvent;
} }

Note: You need to add this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); on your activity or else keyboard will popup at first time.

查看更多
登录 后发表回答