java - 如何在editText中放置一个按钮

标签 java android material-design material-components material-components-android

我正在制作一个聊天应用程序,我想在 editText 中放置一个按钮以启用媒体文件的发送。我不知道该怎么办。

尝试使 editText 中的 vector 资源可点击,但没有成功

最佳答案

可以使用官方TextInputLayout组件。

您可以使用 app:endIconMode="custom" 属性自定义要使用的图标,并使用 app:endIconDrawable 指定可绘制对象.

类似于:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/custom_end_icon"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/hint_text"
    app:endIconMode="custom"
    app:endIconDrawable="@drawable/custom_icon"
    app:endIconContentDescription="@string/custom_content_desc">

  <com.google.android.material.textfield.TextInputEditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

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

您可以设置 TextInputLayout#setEndIconOnClickListener处理点击的方法。

TextInputLayout textInputLayout = findViewById(R.id.custom_end_icon);
textInputLayout.setEndIconOnClickListener(new View.OnClickListener() {
          @Override public void onClick(View view) {
            // do something.
          }
        });

enter image description here enter image description here

关于java - 如何在editText中放置一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57689127/

相关文章:

Android 使用 MaterialDatePicker 和 TextInputLayout

android - CollapsingToolbarLayout 中的多行 TextView 而不是标题

java - 尝试音频剪辑

java - 当数组长度大于5时程序仍然执行

javascript - vue-material stepper 提交

android - OpenGL Point Sprites 在 Android 中工作吗?

android - 使用 viewLifecycleOwner 的生命周期范围从 Fragment 启动协程

java.lang.NoClassDefFoundError : com/google/gwt/core/ext/Generator 错误

Java项目查找非限定字符串引用的类--请求评论

android - 旋转动画在 android 4.0 中不起作用