Disable EditText editability and focus (like TextView)

后端 未结 7 1329
说谎
说谎 2021-01-02 03:49

Is there a way to make EditText behaviors like TextView in Android (XML is prefered)? I have tried the following:

 android:editable         


        
相关标签:
7条回答
  • 2021-01-02 04:00

    How many diffrent and wrong ways( Use always

    myEditText.setEnabled(false);
    
    0 讨论(0)
  • 2021-01-02 04:04

    To make EditText not touchable:

    android:enabled="false"
    android:clickable="false"
    android:longClickable="false"
    

    To make EditText look like TextView:

    android:background="@android:color/transparent"

    0 讨论(0)
  • 2021-01-02 04:07

    If you want an EditText not editable or focusable (just like a TextView) but with the correct background, you can use the attribute ?editTextBackground on a TextView. If you use drawables like @android:drawable/edit_text it will not use a background dependent on the api version.

    This:

      <EditText
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />
    

    has the same UI properties of this:

       <TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="?editTextBackground"/>
    
    0 讨论(0)
  • 2021-01-02 04:08

    Now I get a new way. Since I turned off most features of EditText, it should be better to think how to "beautify" the ugly white background of TextView. And the answer is in the source code: Just adding android:background="@android:drawable/edit_text" to a TextView makes the same effects. Thanks again Matthew.

    0 讨论(0)
  • 2021-01-02 04:11

    Disable EditText editability and focus (like TextView).

    Try this :

    android:longClickable="false"
    android:clickable="false"    
    android:cursorVisible="false"
    android:editable="false"
    
    0 讨论(0)
  • 2021-01-02 04:21

    EditText and TextView are quite similar. The only difference I see hard-coded into EditText.java is to set the editable default to true, which you've set manually. Other than that, the EditText style is:

    <style name="Widget.EditText">
        <item name="android:focusable">true</item>
        <item name="android:focusableInTouchMode">true</item>
        <item name="android:clickable">true</item>
        <item name="android:background">@android:drawable/edit_text</item>
        <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
        <item name="android:textColor">@android:color/primary_text_light</item>
        <item name="android:gravity">center_vertical</item>
    </style>
    

    and TextView is:

    <style name="Widget.TextView">
        <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
    </style>
    

    My guess is that @android:drawable/edit_text is the source of the orange box. Indeed, it contains:

    <item android:state_pressed="true" android:drawable="@drawable/textfield_pressed"/>
    

    The simplest way might be to set its background to the default one:

    android:background="@android:drawable/textfield_default"

    0 讨论(0)
提交回复
热议问题