问题
I am making a chat application and I wanted to put a button in an editText to enable the sending of Media files. I don't know how to go about it.
Tried to make the vector asset in the editText clickable but it is not going
回答1:
You can use the official TextInputLayout component.
You can customize the icon to use using the app:endIconMode="custom"
attribute and specifying the drawable with app:endIconDrawable
.
Something like:
<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>
You can set the TextInputLayout#setEndIconOnClickListener method to handle the click.
TextInputLayout textInputLayout = findViewById(R.id.custom_end_icon);
textInputLayout.setEndIconOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
// do something.
}
});
回答2:
Try something like this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1>
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type a message"
android:inputType="text"
android:text="" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/yourImage"/>
</LinearLayout>
来源:https://stackoverflow.com/questions/57689127/how-to-put-a-button-in-an-edittext