如何在editText中放置按钮

Fon*_*nce 2 java android material-design material-components material-components-android

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

试图使 editText 中的矢量资产可点击,但它不会

Gab*_*tti 6

您可以使用官方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>
Run Code Online (Sandbox Code Playgroud)

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

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

在此处输入图片说明 在此处输入图片说明