How to disable EditText in Android

前端 未结 18 1919
后悔当初
后悔当初 2020-12-13 12:03

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

相关标签:
18条回答
  • 2020-12-13 12:21

    Set inputType attribute to none in your layout.xml file under EditText

    0 讨论(0)
  • 2020-12-13 12:22

    Enable:

    private void enableEditText() {
        mEditText.setFocusableInTouchMode(true);
        mEditText.setFocusable(true);
        mEditText.setEnabled(true);
    }
    

    Disable:

    private void disableEditText() {
        mEditText.setEnabled(false);
        mEditText.setFocusable(false);
        mEditText.setFocusableInTouchMode(false);
    }
    
    0 讨论(0)
  • 2020-12-13 12:25

    Write this in parent:

    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    

    Example:

    <RelativeLayout 
    android:id="@+id/menu_2_zawezanie_rl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/menu_2_horizontalScrollView"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:orientation="horizontal"
    android:background="@drawable/rame">
    
    <EditText
        android:id="@+id/menu2_et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/menu2_ibtn"
        android:gravity="center"
        android:hint="@string/filtruj" >
    </EditText>
    <ImageButton
        android:id="@+id/menu2_ibtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:background="@null"
        android:src="@drawable/selector_btn" />
    

    0 讨论(0)
  • 2020-12-13 12:27

    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"

    0 讨论(0)
  • 2020-12-13 12:29
    Edittext edittext = (EditText)findViewById(R.id.edit);
    edittext.setEnabled(false);
    
    0 讨论(0)
  • 2020-12-13 12:37
    edittext.setFocusable(false); 
    edittext.setEnabled(false);
    
    InputMethodManager imm = (InputMethodManager)
      getSystemService(Context.INPUT_METHOD_SERVICE);
    
    imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
      //removes on screen keyboard
    
    0 讨论(0)
提交回复
热议问题