简体   繁体   中英

TextInputEditText - Make clickable but not editable

I have a layout where I want to show a bunvh of edit texts and a date picker. Since I want to have the same design for all fields, I figured out that I need to user an edit text for date picker too but make it non editable but clickable. Below is the xml code of my edit text that will be used to show the date picker:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/notification_layout"
    style="@style/Theme.Connect.InputFieldLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:startIconDrawable="@drawable/ic_stk_notification_24dp">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/notification_edit_text"
        style="@style/Theme.Connect.InputField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Notification"
        android:maxLines="1"
        android:lines="1"
        android:cursorVisible="false"/>

</com.google.android.material.textfield.TextInputLayout>

I tried many solutions I found on the internet, I did managed to make edit text non editable but without the ripple click effect. Is there a way to make the edit text non edditable but still keep the ripple click effect? Is this the best practice to show a date picker consisting the design of the form layout?

You can simply use a TextView instead of an EditText and you can set an onClickListener() to it. You can also style it the way you want.

notification_textview.setOnClickListener {
    // Whatever you want to do after a click
}

You can get a ripple effect on touch by adding these 2 attributes to your textview

<......Textview
android:background=?android:attr/selectableItemBackground
android:clickable="true" />

You can also add styles to a textview that you wanted to use for the editText .

 Try this and change the `android:focusable` attribute to `false`                       


<com.google.android.material.textfield.TextInputLayout
      android:id="@+id/notification_layout"
       style="@style/Theme.Connect.InputFieldLayout"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     app:startIconDrawable="@drawable/ic_stk_notification_24dp">

<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/notification_edit_text"
    style="@style/Theme.Connect.InputField"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Notification"
    android:maxLines="1"
    android:focusable="false"
    android:lines="1"
    android:cursorVisible="false"/>

</com.google.android.material.textfield.TextInputLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM