EditText是Android开发中常用的控件之一,它可以让用户在输入框中输入文字或数字等信息。但是,有时候我们需要在输入框中加入删除功能,方便用户删除输入框中的文字。那么,本文就为大家介绍一下带有删除功能的EditText实例源码。

首先,我们需要在layout文件中添加EditText控件,并添加一个ImageView控件作为删除按钮。代码如下:
```
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入内容"/> android:id="@+id/delete_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_delete" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dp"/>
```
我们在RelativeLayout布局中添加了一个EditText控件和一个ImageView控件。其中,ImageView控件的src属性为删除按钮的图片,通过layout_alignParentRight属性将其放置在输入框的右侧,通过layout_centerVertical属性将其垂直居中,通过layout_marginRight属性设置与右侧的边距为10dp。
接下来,我们需要在Activity中对删除按钮进行操作。具体实现如下:
```
public class MainActivity extends AppCompatActivity {
private EditText editText;
private ImageView deleteText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.edit_text);
deleteText = findViewById(R.id.delete_text);
deleteText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText("");
}
});
}
}
```
在Activity中,我们先通过findViewById方法获取到EditText和ImageView控件,然后通过setOnClickListener方法为ImageView控件设置点击事件。当用户点击删除按钮时,我们通过setText方法将EditText中的内容设置为空字符串,即删除了输入框中的文字。
通过以上代码,我们就实现了带有删除功能的EditText实例。当然,我们还可以对删除按钮进行美化,让其更符合我们的UI风格。