带清除小按钮的输入框

java代码:

public class EditItemView extends FrameLayout {
    TextView itemViewName;
    EditTextWithIcon itemViewValue;
    private String itemHint;
    private String itemName;
    private int valueColor;
    private int nameColor;
    private int nameSize;
    private int desSize;
    View marginDividerView;
    private boolean marginDivider;
    private int gravity;
    public EditItemView(Context context) {
        super(context);
        init(null, 0);
    }

    public EditItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public EditItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.EditItemView, defStyle, 0);
        itemName = a.getString(R.styleable.EditItemView_editName);
        itemHint = a.getString(R.styleable.EditItemView_editHint);

        gravity = a.getInt(R.styleable.EditItemView_android_gravity, Gravity.LEFT|Gravity.CENTER_VERTICAL);
        valueColor = a.getColor(R.styleable.EditItemView_editValueColor, ContextCompat.getColor(getContext(),R.color.black));
        nameColor = a.getColor(R.styleable.EditItemView_editNameColor, ContextCompat.getColor(getContext(),R.color.black));
        nameSize = a.getInt(R.styleable.EditItemView_editNameSize, 16);
        desSize = a.getInt(R.styleable.EditItemView_editDesSize, 14);
        int editInputType = a.getInt(R.styleable.EditItemView_android_inputType, InputType.TYPE_CLASS_TEXT);
        marginDivider = a.getBoolean(R.styleable.EditItemView_marginDivider,false);
        a.recycle();
        View.inflate(getContext(), R.layout.edit_item_view, this);
        itemViewName = findViewById(R.id.itemView_name);
        itemViewValue =  findViewById(R.id.itemView_des);
        marginDividerView =  findViewById(R.id.marginDivider);
        itemViewName.setText(itemName);
        itemViewValue.setHint(itemHint);

        itemViewName.setTextColor(nameColor);
        itemViewValue.setTextColor(valueColor);
        itemViewName.setTextSize(TypedValue.COMPLEX_UNIT_SP,nameSize);
        itemViewValue.setTextSize(TypedValue.COMPLEX_UNIT_SP,desSize);
        itemViewValue.setInputType(editInputType);
        itemViewValue.setGravity(gravity);
        if (gravity == Gravity.RIGHT | gravity ==Gravity.CENTER_VERTICAL | gravity == Gravity.END)
        itemViewValue.setEllipsize(TextUtils.TruncateAt.END);
        marginDividerView.setVisibility(marginDivider?VISIBLE:GONE);
    }

    public void setTitle(String title) {
        itemViewName.setText(title);
    }

    public void setItemValue(String des) {
        itemViewValue.setText(des);
    }
    public String getName(){
        return itemViewName.getText().toString();
    }
    public String getItemValue(){
        return StringUtils.isEmpty(itemViewValue.getText().toString())?"":itemViewValue.getText().toString();
    }

    public void setValueColor(int color){
        itemViewValue.setTextColor(ContextCompat.getColor(getContext(),color));
    }

    @Override
    public void setOnFocusChangeListener(OnFocusChangeListener l) {
        itemViewValue.setOnFocusChangeListener(l);
    }

    public EditTextWithIcon getItemViewValue() {
        return itemViewValue;
    }
}

attrs.xml

 <declare-styleable name="EditItemView">
        <attr name="editName" format="string"/>
        <attr name="editHint" format="string"/>
        <attr name="editValueColor" format="reference"/>
        <attr name="editNameColor" format="reference"/>
        <attr name="editNameSize" format="integer"/>
        <attr name="editDesSize" format="integer"/>
        <attr name="android:inputType"/>
        <attr name="android:gravity"/>
        <attr name="marginDivider"/>
    </declare-styleable>
edit_item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/bg_white_item"
    >
    <LinearLayout
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_white_item">
        <TextView
            android:layout_marginLeft="@dimen/dp_16"
            android:id="@+id/itemView_name"
            android:minHeight="48dp"
            android:gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <com.xx.widget.EditTextWithIcon
            android:textColor="@color/black"
            android:singleLine="true"
            android:gravity="center_vertical"
            android:id="@+id/itemView_des"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:minHeight="48dp"
            android:paddingRight="16dp"
            android:paddingLeft="@dimen/dp_10"
            android:inputType="text"
            android:background="@color/white"
            />

    </LinearLayout>
    <View
        android:id="@+id/marginDivider"
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="@color/divider_gray"
        android:layout_marginStart="@dimen/dp_16"
        android:layout_marginEnd="@dimen/dp_16"
        android:visibility="gone"/>
</LinearLayout>
EditTextWithIcon.java 代码
/**
 * 带有图标和删除符号的可编辑输入框,用户可以自定义传入的显示图标
 * 
 * @author 
 * 
 */
public class EditTextWithIcon extends AppCompatEditText implements OnTouchListener, TextWatcher, View.OnFocusChangeListener
{

	// 删除符号
	Drawable rightImage = getResources().getDrawable(R.drawable.ic_cancel_black_24dp);

	Drawable icon;
	private OnIconClick onIconClick;

	public EditTextWithIcon(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public EditTextWithIcon(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public EditTextWithIcon(Context context) {
		super(context);
		init();
	}

	private void init() {
		setOnFocusChangeListener(this);
		this.setOnTouchListener(this);
		addTextChangedListener(this);
		rightImage.setBounds(0, 0, rightImage.getIntrinsicWidth(), rightImage.getIntrinsicHeight());
		manageClearButton();
	}

	/**
	 * 传入显示的图标资源id
	 * 
	 * @param id
	 */
	public void setIconResource(int id) {
		icon = getResources().getDrawable(id);
		icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
		manageClearButton();
	}

    /**
     * 传入右边图标资源id
     * @param id
     */
    public void setRightImage(int id) {
		rightImage = getResources().getDrawable(id);
		rightImage.setBounds(0, 0, rightImage.getIntrinsicWidth(), rightImage.getIntrinsicHeight());
        manageClearButton();
    }

	public void manageClearButton() {
		if (this.getText().toString().equals(""))
			removeClearButton();
		else
			addClearButton();
	}

	public void removeClearButton() {
		if (icon ==null)
			icon = this.getCompoundDrawables()[0];
		this.setCompoundDrawables(this.icon, this.getCompoundDrawables()[1], null, this.getCompoundDrawables()[3]);
	}

	void addClearButton() {
		if (icon ==null)
			icon = this.getCompoundDrawables()[0];
		this.setCompoundDrawables(this.icon, this.getCompoundDrawables()[1], rightImage,
				this.getCompoundDrawables()[3]);
		setCompoundDrawablePadding(10);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		EditTextWithIcon et = EditTextWithIcon.this;

		if (et.getCompoundDrawables()[2] == null)
			return false;
		switch (event.getAction()){
			case MotionEvent.ACTION_DOWN:
				if (onIconClick != null){
					onIconClick.onIconClick(event.getAction());
				}
				break;
			case MotionEvent.ACTION_UP:
				if (event.getX() > et.getWidth() - et.getPaddingRight() - rightImage.getIntrinsicWidth()) {
					if (onIconClick == null){
						et.setText("");
						this.removeClearButton();
					}else {
						onIconClick.onIconClick(event.getAction());
					}
				}
				break;
		}

		return false;
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		this.manageClearButton();
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {

	}

	@Override
	public void afterTextChanged(Editable s) {

	}

	public void setOnIconClick(OnIconClick onIconClick)
	{
		this.onIconClick = onIconClick;
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus)
	{
		if (hasFocus)
		{
			manageClearButton();
		}
		else removeClearButton();
	}

	public interface OnIconClick{
		void onIconClick(int action);
	}

}

使用方式:

  tvIn.setOnClickListener(v -> {
           v.setTag(tvEditMoney.getItemValue());
            String money = (String) v.getTag();

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值