先上效果图
实现:
1.先编写xml文件,在layout文件夹下新建view_edittext文件
主要就是一个EditText和一个Button,其他样式可以自己定,其中删除按钮的图片,可以去阿里图标里搜索删除,作为button的background就可以了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@color/white">
<EditText
android:id="@+id/et_view"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_weight="4"
android:background="@null"
android:gravity="center_vertical"
android:singleLine="true"
android:textColor="#0e0e0e"
android:textColorHint="#b0c6ce"
android:textSize="14sp" />
<Button
android:id="@id/bt_clear"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|center_vertical"
android:layout_margin="10dp"
android:padding="5dp"
android:background="@drawable/cross" />
</LinearLayout>
</LinearLayout>
2.编写CustomizeEditText,继承LinearLayout,实现View.onClickListener和TextWatch接口
public class CustomizeEditText extends LinearLayout implements View.OnClickListener, TextWatcher {
private EditText editText;
private Button clearButton;
//设置editText的提示文字
public void setEditTextHint(String hint){
if(editText!=null){
editText.setHint(hint);
}
}
//获得EditText的文字
public String getText(){
return editText.getText().toString();
}
public CustomizeEditText(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.view_edittext,this,true);
editText = findViewById(R.id.et_view);
editText.addTextChangedListener(this);
clearButton = findViewById(R.id.bt_clear);
clearButton.setVisibility(GONE);
clearButton.setOnClickListener(this);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
//当输入框文字有改变时
@Override
public void afterTextChanged(Editable s) {
String input = editText.getText().toString();
if(input.isEmpty()){
clearButton.setVisibility(GONE);
}else {
clearButton.setVisibility(VISIBLE);
}
}
@Override
public void onClick(View v) {
//当点击了清空按钮,则清空editText的内容
switch (v.getId()){
case R.id.bt_clear:
editText.setText("");
break;
}
}
public EditText getEditText(){
return editText;
}
}
3.在Activity中使用
activity的xml:
主要就是将customizeEditText放进去,其他样式自己定
<com.xx.xx.customUI.CustomizeEditText
android:id="@+id/ed_work_id"
android:layout_width="match_parent"
android:layout_height="@dimen/control_big_margin"
android:hint="@string/ed_hint_work_id"
android:textColorHint="@color/light_gray"
android:background="@null"
android:padding="10dp"
android:textColor="@color/black"
android:maxLength="5"
android:maxLines="1"/>
在activity中使用的代码
private CustomizeEditText workIdEditText;
//绑定activity中xml的customizeEditText
workIdEditText = findViewById(R.id.ed_work_id);
workIdEditText.setEditTextHint("请输入工号");
//获得editText输入的内容
String result = workIdEditText.getText();
注意:如果要动态修改editText的样式,得在CustomizeEditText中动手