简体   繁体   中英

TextInputLayout - make it clickable with working ripple effect

I want that my TextInputLayout has following functions:

  • open a dialog on click
  • show a ripple background like eg the ExposedDropdownMenu style does

It should look like a TextInputLayout but work like a Button .

Code

What I have so far is following:

 <com.google.android.material.textfield.TextInputLayout
    android:id="@+id/tilPlannedStartTime"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/planned_start_time"
    android:padding="8dp">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/tielPlannedStartTime"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:editable="false"
        android:clickable="true"
        android:focusable="false"
        android:singleLine="true" />

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

And following:

binding.actvPlannedStartTime.setText(workout.item.plannedStartTime?.toString(SimpleTime.Format.DisplayHM) ?: "-")
    binding.tielPlannedStartTime.setOnClickListener {
        Toast.makeText(it.context, "TIEL", Toast.LENGTH_SHORT).show()
    }
    binding.tilPlannedStartTime.setOnClickListener {
        Toast.makeText(it.context, "TIL", Toast.LENGTH_SHORT).show()
    }
    

Problem

The click event does not have any visual feedback (ripple) like eg the ExposedDropdownMenu style would have. Any ideas how to solve this better?

you can use android:background="?attr/selectableItemBackground" , but this will remove your current background (so probably also frame around EditText ). some workaround may be to wrap your lines in FrameLayout , make it clickable ( OnClickListener for it) and with attr android:foreground="?attr/selectableItemBackground" ( foreground is available only for FrameLayout , TextInputLayout extends LinearLayout sadly and don't have foreground...)

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