How to disable EditText in Android

2020-02-07 19:24发布

How can I disable typing in an EditText field in Android?

16条回答
Ridiculous、
2楼-- · 2020-02-07 19:58
Edittext edittext = (EditText)findViewById(R.id.edit);
edittext.setEnabled(false);
查看更多
混吃等死
3楼-- · 2020-02-07 19:59

you can use EditText.setFocusable(false) to disable editing
EditText.setFocusableInTouchMode(true) to enable editing;

查看更多
Emotional °昔
4楼-- · 2020-02-07 19:59

According to me...if you have already declared the edittext in the XML file and set the property in the XML file "android:enabled=false" and disable at runtime to it at that time in java file adding only 2 or 3 lines

  1. First create the object
  2. Declare EditText et1;
  3. Get by id

    et1 = (EditText) FindViewById(R.id.edittext1);
    et1.setEnabled(false);

You can do enable or disable to every control at run time by java file and design time by XML file.

查看更多
狗以群分
5楼-- · 2020-02-07 20:00
edittext.setFocusable(false); 
edittext.setEnabled(false);

InputMethodManager imm = (InputMethodManager)
  getSystemService(Context.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
  //removes on screen keyboard
查看更多
三岁会撩人
6楼-- · 2020-02-07 20:02

Just set focusable property of your edittext to "false" and you are done.

<EditText
        android:id="@+id/EditTextInput"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:gravity="right"
        android:cursorVisible="true">
    </EditText>

Below options doesn't work

In code:

editText.setEnabled(false); Or, in XML: android:editable="false"

查看更多
做自己的国王
7楼-- · 2020-02-07 20:03

In code:

editText.setEnabled(false);

Or, in XML:

android:editable="false"
查看更多
登录 后发表回答