带有删除按钮的编辑框

xiaoxiao2021-02-28  82

//自定义待删除按钮控件代码 import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.support.annotation.Nullable; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import com.zk.myappdemo.R; /** * 日期:2017/5/5 13:26 * 描述:带删除按钮控件 */ public class DelEditText extends LinearLayout { EditText edit;//编辑框 ImageView image;//右边图片 删除用 int color;//字体颜色 float textSize;//字体大小 String texthint;//提示信息 int imageId; public DelEditText(Context context) { super(context, null); } public DelEditText(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initView(context, attrs);//初始化 操作 } public DelEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } private void initView(Context context, AttributeSet attrs) { //加载布局 LayoutInflater.from(context).inflate(R.layout.custom_h_line, this, true); edit = (EditText) findViewById(R.id.et); image = (ImageView) findViewById(R.id.iv_del); //获取自定义属性 TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.DelEditText); color = typeArray.getColor(R.styleable.DelEditText_EditTextColor, Color.BLACK); textSize = typeArray.getDimensionPixelSize(R.styleable.DelEditText_EditTextsize1, 15); texthint = typeArray.getString(R.styleable.DelEditText_EditHint); imageId = typeArray.getResourceId(R.styleable.DelEditText_Image, R.mipmap.del); //添加方法 setEditTextColor(color);//设置颜色 setEditTextSize(textSize);//设置字体大小 setEditHint(texthint);//设置提示信息 setEditImage(imageId);//设置删除图片 edit.addTextChangedListener(textW);//EditText的输入内容改变监听 //image view的点击监听 image.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { edit.setText(""); image.setVisibility(View.GONE); } }); typeArray.recycle(); } /** * 日期:2017/5/5 17:16 * 描述:设置删除按钮图片 * 作者:hxy */ public void setEditImage(int imageId) { image.setImageResource(imageId); } /** * 日期:2017/5/5 17:16 * 描述:设置输入提示 * 作者:hxy */ public void setEditHint(String texthint) { edit.setHint(texthint); } /** * 日期:2017/5/5 17:16 * 描述:设置字体大小 * 作者:hxy */ public void setEditTextSize(float textSize) { edit.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize); } /** * 日期:2017/5/5 17:17 * 描述:设置字体颜色 * 作者:hxy */ public void setEditTextColor(int color) { edit.setTextColor(color); } //返回EditText控件 public EditText getEditText() { return edit; } //返回EditText中输入的内容 public String getText() { return edit.getText().toString(); } //设置EditText中内容 public void setText(int text) { edit.setText(text); } //设置EditText中内容 public void setText(String text) { edit.setText(text); } /** * 描述:编辑框改变监听 * 作者:hxy */ TextWatcher textW = new TextWatcher() { @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) { if (s.length() == 0) {//没有输入隐藏按钮 image.setVisibility(View.GONE); } else { image.setVisibility(View.VISIBLE); } } }; }

布局中使用

<com.zk.myappdemo.view.DelEditText android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/color_control_normal" app:EditHint="请输入" app:EditTextColor="#0000ff" app:EditTextsize1="12sp"/>

自定义属性

<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="DelEditText"> <attr name="EditTextColor" format="color"/> <attr name="EditTextsize1" format="dimension"/> <attr name="EditHint" format="string"/> <attr name="Image" format="reference"/> </declare-styleable> </resources>

转载请注明原文地址: https://www.6miu.com/read-36436.html

最新回复(0)